Ahhh, ok! I was missing two important bits of info: 1) I didn't know about the backref() function. I was just using backref='users' in my relationship() args. 2) Thus, I didn't realize that using cascade_backrefs=False as an arg to relationship() would apply the setting in the wrong direction.
Using "backref=backref('users', cascade_backrefs=False)" has fixed my problem. Thanks for the quick response! On Jun 23, 4:34 pm, Michael Bayer <mike...@zzzcomputing.com> wrote: > On Jun 23, 2011, at 7:12 PM, Robert Rollins wrote: > > > > > I'm using SQLAlchemy 0.7.1 on a MySQL 5.1.57 database, and I'm getting > > unexpected behavior with the cascade_backrefs=False parameter for my > > many-to-many relationships. > > > groups = relationship(ZKGroup, secondary=user_groups, > > backref='users', cascade_backrefs=False) > > > def __init__(self, username, groups) > > self.username = username > > self.groups = groups > > > Now, I would expect the cascade_backrefs=False option to prevent newly > > created ZKUser objects with a persistent ZKGroup object in their > > 'groups' list to not be added to the session. But the ZKUser object > > appears to being getting added to the session anyway. I can tell > > because this code: > > > Is this a bug with cascade_backrefs=False? Or is it just not meant to > > be used with many-to-many relationships, or something like that? > > I was pretty sure this came down to which side you put the flag on, keeping > in mind that *I* didn't know which side it should be on. Apparently its > the side that would be receiving cascades via a backref, not the one that > would be sending them: > > groups = relationship(ZKGroup, > secondary=user_groups, > backref=backref('users', cascade_backrefs=False)) > > That is, the event is initiated by "self.groups", which then hits group.users > on the backref, but then the new User is not cascaded due to the flag. > > The docstring for relationship() is accurate here, from the perspective that > the "cascade_backrefs" goes on the side that would be receiving the event, > not the one initiating it, but it only speaks in terms of > one-to-many/many-to-one, so even with all that typing I did it's still not > clear. But with backref events, it doesn't really matter what type of > pattern is on each side. > > The documentation for the flag in the "Session / Cascades" section, well > looking there it looks like the mappings are nonsensical, geez....fixing that > now. > > > > > -- > > 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 > > sqlalchemy+unsubscr...@googlegroups.com. > > For more options, visit this group > > athttp://groups.google.com/group/sqlalchemy?hl=en. -- 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 sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.