Chapter 12.3.3. SQLObject Composition vs. Inheritance

SQLObject obviously supports composition out of the box. You can have a Vehicle, Car, Airplane, and Submarine table. Every row in the Car, Airplane, and Submarine table will contain a foreign key column that will point to the Vehicle table:

class Vehicle(SQLObject):
    model = StringCol()
    make = StringCol()
    year = StringCol()
    color = StringCol()
    owner = StringCol()

class Car(SQLObject):
    vehicle = ForeignKey('Vehicle')
    doors = IntCol()

v = Vehicle(make='Ford', model='Mustang', year='1978', color='Red',
owner='SpongeBob SquarePants')
c = Car(vehicle=v.id, doors=5)

print c
print c.vehicle.make, c.vehicle.model, c.vehicle.year, c.vehicle.color, c.vehicle.
owner, c.doors

Output:

Ford Mustang 1978 ...

Get Rapid Web Applications with TurboGears: Using Python to Create Ajax-Powered Sites now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.