On Sep 9, 2007, at 7:00 PM, Dan Eloff wrote:
> > Hi All, > > Table Categories: > > categories_table = sa.Table('categories', connection.metadata) > [categories_table.append_column(c) for c in ( > sa.Column('id', sa.Integer, primary_key=True), > sa.Column('name', sa.String(40)), > sa.Column('flags', sa.SmallInteger), > sa.Column('parent_id', sa.Integer, sa.ForeignKey > ('categories.id')))] > > sa.orm.mapper(Category, categories_table, properties={ > 'subcategories' : sa.orm.relation(Category, > backref=sa.orm.backref('_parent', > remote_side=[categories_table.c.id])), > }) > > c = Category() > c.subcategories.append(Category()) # works great > c._parent.append(Category()) # fails, c._parent is NoneType??? > > Why isn't _parent a list? This is actually a one-to-many so there is > only one parent, but assigning doesn't work either: the relation from Category to _parent is many-to-one, so its a scalar in that direction (a one-to-many is always many-to-one in the reverse direction, and vice versa) > > c._parent = Category() > > Traceback (most recent call last): > File "C:\Program > Files\Python\lib\site-packages\sqlalchemy-0.4.0beta5-py2.5.egg > \sqlalchemy\orm\attributes.py", > line 76, in __set__ > File "C:\Program > Files\Python\lib\site-packages\sqlalchemy-0.4.0beta5-py2.5.egg > \sqlalchemy\orm\attributes.py", > line 364, in set > File "C:\Program > Files\Python\lib\site-packages\sqlalchemy-0.4.0beta5-py2.5.egg > \sqlalchemy\orm\attributes.py", > line 300, in fire_replace_event > File "C:\Program > Files\Python\lib\site-packages\sqlalchemy-0.4.0beta5-py2.5.egg > \sqlalchemy\orm\attributes.py", > line 130, in sethasparent > AttributeError: 'list' object has no attribute '_state' I cant reproduce this, although the error to me seems like you are actually saying c._parent = [] (some list object). If thats not it, send along a reproducing test script. > > I want to be able to create the tree of Category() objects from the > top down and set relations via _parent, but how can I do this? just the way you are doing it, c._parent = Category(). works for me on this end... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---