this question is arising with unusual frequency in recent weeks, which suggests 
a shortcoming in the docs.  Rather than answering here, I've added what is 
hopefully a comprehensive answer to the documentation; it would be helpful if 
you could confirm for me that it answers your question, or if it needs further 
detail:

http://docs.sqlalchemy.org/en/rel_0_7/orm/relationships.html#deleting-rows-from-the-many-to-many-table

Also, note that the ORM is not aware that your PostCategory.__table__ is both a 
mapped entity as well as a "secondary" at the same time.   If you deal with 
PostCategory entities explicitly, they will be stale within your Session until 
the next commit or expiration, if the corresponding "secondary" table row has 
been altered or deleted.  This is why "secondary" does not accept a mapped 
entity directly as an argument - it isn't entity aware.



On Jun 25, 2012, at 4:09 PM, Gerald Thibault wrote:

> I have a m2m relationship of Posts and Categories. A post can be in any 
> number of categories, and a category can have any number of posts. This 
> relation is defined on the Post model:
> 
> categories = relation(Category, lazy=True, secondary=PostCategory.__table__,
>                           backref=backref('posts'))
> 
> PostCategory looks like this:
> 
> class PostCategory(orm.Base):
>     post_id = Column(Integer, ForeignKey('posts.id'), primary_key=True)
>     category_id = Column(Integer, ForeignKey(Category.id), primary_key=True)
> 
> When either of these two objects are deleted, I want to purge all records 
> involving that object from the association table. Doing this manually is not 
> an issue, but I'd like to code this behavior right into the class so as to 
> keep the logic outside of the views.
> 
> I am using MyISAM on mysql, so the relevant section on 'ON DELETE CASCADE' 
> isn't useful here.
> 
> Anyone know how to accomplish this?
> 
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To view this discussion on the web visit 
> https://groups.google.com/d/msg/sqlalchemy/-/J6XhNh1fZGMJ.
> 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.

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

Reply via email to