[Twisted-Python] relationships in t.e.row
sean at twistedmatrix.com
Sat Sep 7 17:35:12 EDT 2002
a natural extension of twisted.enterprise.row is to add support for
relationships between tables.
anyone got any good examples of tools or apps that do this well?
Some initial thoughts..
Currently, the reflector is initialized with the "stubs" structure that
stubClass, tablename, keycolumns
for each (database table / row class). This information could be extended to
include some relationship data in the form of foreign keys. This foreign key
data would allow relationships between tables in the database to be mirrored
into the reflector.
For example, in the existing row example code the stubs structure is:
stubs = [ (RoomRow, "testrooms", [("roomId","int4")]) ]
If there was another table "furniture" and a foreign key in that table that
connects it to the room table, then that could be specified when
initializing the reflector:
stubs = [
(RoomRow, "testrooms", [("roomId","int4")]),  ),
(FurnitureRow, "furniture", [("furnId","int4")]), [("roomId", "int4",
"rooms", "roomId")] )]
where the forth element in each entry is a foreign key of :
(could specify the foreign class also, but the reflector should already know
which class corresponds to the foreign table.)
This foreign key data would then be used when loading row objects from the
database. Any time a "RoomRow" object is loaded, it would load all of the
FurnitureRows for that room. Although the foreign key data is specified for
the child table (furniture) it is when objects of the parent table
(testrooms) are loaded that the significant work happens.
Allowing an option for child rows to be loaded when loading a row is
probably a good idea.
This probably requires some changes to t.e.row to work. Currently the
optional "factoryMethod" is passed into Reflector::loadObjectsFrom. The
factoryMethod will need to be available for children classes also, so it
probably should be added to the stubs structure.
The "data" passed to loadObjectsFrom is also an issue, maybe the data passed
into the highest level loadObjectsFrom is passed down to children, or the
parent Row instance that the rows are being loaded for is passed as the
data...? Maybe loadObjectsFrom takes a parent object every time it is
called - this may even be required.
The relationship between rows once they are loaded is another issue. Where
are children row objects put once they are loaded? In the above example,
maybe the RoomRow object has a list member variable called "furniture" or
"children" added to it and the FurnitureRow objects are put in that list?
This becomes a type of containment system with a heirarchy of containers...
So, loading row objects is doable. I'm not sure of all the implications for
other SQL operations. Assuming that all changes to the database are going
through the row interface makes things easier, but it requires more
thought... A good goal is to be able to operate on these row objects in
memory and have the changes propagate into the database in controllable
"If he's so smart, then how come he's dead?", Homer Simspon
sean at ninjaneering.com
More information about the Twisted-Python