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 <[email protected]> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.
signature.asc
Description: Message signed with OpenPGP using GPGMail
