Thank you Michael. Yes, that is indeed the case if I delete an object via session.delete().
Unfortunately, it seems that if I execute the DELETE manually against the table in question, that the delete does not cascade, as shown by this code: http://paste.openstack.org/show/66005/ Does this mean that cascades are only handled in the session and are not handled by SQL schema constructs on the tables themselves? Best, -jay On Monday, February 10, 2014 12:16:37 AM UTC-5, Michael Bayer wrote: > > the problem is this: > > org = Organization(name='my org') > sess.add(org) > > group = Group(name='my group') > group.organization_id = org.id > sess.add(group) > > when you assign group.organization_id = org.id, org has not been flushed > yet so org.id is None. The org is then never associated with the group. > Setting echo=True is the best way to quickly analyze these issues. > > It’s a good idea to work with object references if at all possible to > avoid issues like these: > > org = Organization(name='my org') > sess.add(org) > > group = Group(name='my group') > group.organization = org > sess.add(group) > > user = User(name='my user') > sess.add(user) > sess.commit() > > group_membership = UserGroupMembership(user=user, group=group) > sess.add(group_membership) > sess.commit() > > assert sess.query(UserGroupMembership).count() == 1 > > sess.delete(org) > sess.commit() > > assert sess.query(UserGroupMembership).count() == 0 > > > > > On Feb 9, 2014, at 10:45 PM, Jay Pipes <jayp...@gmail.com <javascript:>> > wrote: > > Hi all, > > I have some models with the following relationship: > > An Organization can have many Groups > A Group belongs to one Organization > A User can belong to many Groups > A Group can have many Users > > I use a mapping model called UserGroupMembership that stores the user_id, > group_id relation of the many-to-many user to groups relationship. > > I cannot seem to get cascading deletes to work properly for the mapping > table. > > If I create an organization, a group under that organization, a user, and > add a record to the mapping table relating the user to the group, I would > expect that deleting the organization would delete the group record, as > well as the records in the mapping table that correspond to the group. > > Unfortunately, what actually happens is that the organization is deleted, > along with the group record. But the mapping table records corresponding to > that group are not deleted -- leaving orphan records. > > Here is code that demonstrates this: > > http://paste.openstack.org/show/63736/ > > If you place the above code into a file called manydelete.py and execute > it, you will see an assertion raised: > > $ python manydelete.py > Traceback (most recent call last): > File "manydelete.py", line 127, in <module> > assert len(recs) == 0 > AssertionError > > Note: In case anyone is wondering, the reason I use sess.commit() a number > of times is to emulate what my code is doing (in multiple processes hitting > the database in different API calls...) > > I'm wondering if I have set up the cascading bits or the relation()s > improperly? Any help would be most appreciated! > > Best, > -jay > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sqlalchemy+...@googlegroups.com <javascript:>. > To post to this group, send email to sqlal...@googlegroups.com<javascript:> > . > Visit this group at http://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.