On Jan 9, 2014, at 10:02 AM, Russell Holloway <russ.d.hollo...@gmail.com> wrote:

> 
> you need to put a cascade rule on Page.user_relationships, such that when you 
> remove a Page_to_User from the collection, it’s marked as deleted, instead of 
> SQLAlchemy setting the page_id foreign key to NULL, which is invalid here 
> b.c. that column is part of the primary key (and hence the error).   
> Page_to_User can’t exist in the database without being referred to by a Page 
> object since the primary key would be NULL.
> 
> the delete-orphan cascade is introduced at:
> 
> http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html#configuring-delete-delete-orphan-cascade
> 
> and some more information at: 
> http://docs.sqlalchemy.org/en/rel_0_9/orm/session.html#deleting-from-collections
> 
> 
> 
> Can you help me understand why a cascade rule is needed? I have read the 
> documentation you linked to but still have trouble grasping why it is needed. 
> I understand it would make sense if I deleted a page, that it should cascade 
> on delete to page_to_user because now the FK page_id is no longer valid - the 
> page doesn't exist, so it cannot possibly be mapped anywhere.

OK so, you have:

Page.user_relationships -> collection of PageToUser

PageToUser -> single User

then, you are saying :

some_page.user_relationships = []
session.flush()

What SQL would you expect this to produce?  After a flush, what would the rows 
in your page_to_user table look like?


> 
> What I'm having trouble understanding is what is cascading from what to what 
> when deleting from this page.user_relationships collection. It doesn't 
> cascade from page, because I am not deleting any pages, and it doesn't 
> cascade from users, because no users are deleted.

“delete-orphan” means when an item is removed from a collection, in this case a 
PageToUser object, it is marked as deleted.


> In the database table definition, there doesn't have to be any cascade 
> settings at all for the constraints and it will still work fine.

OK you need to show what “works fine” is - how the Page.user_relationships 
collection can be empty on a particular Page object, yet there are PageToUser 
objects in the database which refer to that Page (or if you think the 
PageToUser row still exists, but doesn’t point to any Page, show me how that 
looks).  What’s in the database?


Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to