itll probably work better with a backreference; that way the two-way relationship of "parent" and "child" is better managed (its also more succinct syntactically).
the docs should probably be changed to illustrate this example since its more common than the "root" example it shows. in the SA distribution theres an "examples" directory which has three self-referential example scripts. both the "examples/adjacencytree/ basic_tree.py" and the "examples/backref/backref_tree.py" examples have the "parent"/"childlist" two-way relationship managed by a backref (these two examples are actually somewhat redundant and should be grouped together). so you would do it like this: mapper(Project, projects, properties={ 'child_projects':relation( Project, primaryjoin=projects.c.id==projects.c.parent_project_id, foreignkey=projects.c.parent_project_id, backref=backref('parent_project', foreignkey=projects.c.id) ) } ) the "byroot_tree" example is much more intricate example than the previous two, and is more like the one in the documentation; if you ask why the "byroot_tree" example doesnt use a backref, its because the node doesnt have a "parent" attribute, it has a "root" attribute instead which indicates the rootmost node in the node's subtree, and which is managed by some explicit code in the TreeNode class itself as well as a mapper extension. so dont worry about that one. On Jun 6, 2006, at 11:23 PM, Randall Smith wrote: > I have a table with a foreign key to itself. The foreign key > references > it's parent record. I would like the table mapped to an object > that has > both a parent and children attribute. I've been going by the treenode > example in the docs. I can get either children to work or parent to > work, but not both. In other words, if I say parent.append(child), > child.parent is NULL. If I say child.parent = parent, parent.children > is an empty list. > > My table def and mapper: > > projects = Table('projects', metadata, > Column('id', INTEGER, primary_key=True), > Column('title', VARCHAR(200), nullable=False), > Column('county_code', INTEGER, nullable=False), > Column('pws_id', INTEGER), > Column('project_type', VARCHAR(100), PassiveDefault('Standard'), > nullable=False), > Column('description', TEXT), > Column('reviewer_id', INTEGER, nullable=False), > Column('reviewer_name', VARCHAR(100), nullable=False), > Column('pro_engineer_id', INTEGER), > Column('pro_engineer_name', VARCHAR(100)), > Column('notes', TEXT), > Column('parent_project_id', INTEGER, > ForeignKey("%s.projects.id" % schemaname)), > Column('input_time', TIMESTAMP, timestamp_default, > nullable=False), > schema=schemaname > ) > > mapper(Project, projects, > properties={ > 'parent_project':relation(Project, > primaryjoin=projects.c.id==projects.c.parent_project_id, > foreignkey=projects.c.id), > 'child_projects':relation(Project, > primaryjoin=projects.c.id==projects.c.parent_project_id, > foreignkey=projects.c.parent_project_id) > } > ) > > Any pointers appreciated. > > Randall > > > > _______________________________________________ > Sqlalchemy-users mailing list > Sqlalchemy-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users _______________________________________________ Sqlalchemy-users mailing list Sqlalchemy-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users