On Oct 1, 2008, at 6:42 AM, Adam Dziendziel wrote:
>
> I have a ContentObject class mapped using:
>
> co_mapper = orm.mapper(ContentObject,
> content_objects_table,
> extension=COMapperExtension(),
> polymorphic_on=content_objects_table.c.type,
> polymorphic_identity='ContentObject',
> properties={
> 'theme': orm.relation(Theme),
> 'resources': orm.relation(Resource,
> secondary=co_resources_table),
> 'children': orm.relation(ContentObject,
> secondary=co_children_table,
>
> primaryjoin=content_objects_table.c.id==co_children_table.c.parent_id,
>
> secondaryjoin
> =co_children_table.c.child_id==content_objects_table.c.id,
> backref='parents'),
> 'translations': orm.relation(ContentObjectTranslation,
> backref='co')
> }
> )
>
> co = ContentObject()
> co.children.append(Session.query(ContentObject).get(1))
> # assert co not in Session # fails!
> co.theme = Session.query(Theme).get(1) # Session flushes automatically
> when retrieving a theme, and I get an error because the theme_id
> column (with nullable=False) is not set yet
>
> IntegrityError: (IntegrityError) content_objects.theme_id may not be
> NULL u'INSERT INTO content_objects (type, parent_id, position,
> theme_id, data, keywords) VALUES (?, ?, ?, ?, ?, ?)' ['group', None,
> None, None, None, '']
>
> If I remove backref='parents' it works as expected. Do I have to turn
> auto-flush off to avoid incidental flushes?
If you're on 0.5, you can in fact disable "save-update" cascade on the
backref, and it should work as planned, using
backref=backref('parent', cascade=None).
Though lately I've just been using a @disable_autoflush decorator for
this situation, in a web app typically there's one controller method
that does the "populate" operation so that works out OK.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---