On Jan 8, 2014, at 5:41 PM, Russell Holloway <russ.d.hollo...@gmail.com> wrote:

> Hello all, 
> 
> I keep hitting an assertion error, "Dependency Rule Tried To Blank Out 
> Primary Key..." when trying to remove all children using an association 
> object.
> 
> My situation seems very similar to 
> https://groups.google.com/forum/#!topic/sqlalchemy/3g4__pFHZTs
> 
> However, based on Michaels response, it sounds like we must delete both 
> objects, which I don't want to do since it is a many-many relationship. Below 
> is a simple equivalent to my code:
> 
> Page(Object):
>   page_id = Column(Integer, primary_key = True)
>   title = Column(String)
> 
>   user_relationships = relationship(Page_to_User)
> 
> User(Object):
>   user_id = Column(Integer, primary_key = True)
>   name = Column(String)
> 
> Page_to_User(Object):
> 
>   page_id = Column(Integer, ForeignKey(Page.page_id), primary_key = True)
>   user_id = Column(Integer, ForeignKey(User.user_id), primary_key = True)
>   relationship_type = (Integer, ForeignKey(Relationship.type_id), primary_key 
> = True)
> 
>   page = relationship(Page)
>   user = relationship(User)


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




> 
> -- 
> 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.

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

Reply via email to