Am Donnerstag, 12. Februar 2015 22:16:37 UTC+1 schrieb Michael Bayer: > > > > Torsten Irländer <torsten....@googlemail.com <javascript:>> wrote: > > > > > Nothing is wrong with the IntegrityException if this Exception is > actually raised :) That is currently my problem: I except such an exception > but it isn't raised. Instead the group is deleted (including all entries > for this group in the secondary table) although still having users > referencing the group. > > There’s no integrity constraint applicable to an association table so > you’d need to catch this in Python. If these are Session.delete() > operations, then you’d need to check the object and assert that its user > collection is empty within a before_flush() event. >
First, thanks for your fast replies Michael! Do I understand it correctly that in contrast to plain SQL (Invoking the delete command in e.g psql), there is no way to make the database side constraints in the association table applicable in SQLAlchemy if I configure this table in the relation using the secondary attribute? So even if there is a constraint in the association table (Foreign key must not become null) this is ignored by SQLAlchemy. I assume that this is because of the unique behavior described here: http://docs.sqlalchemy.org/en/latest/orm/basic_relationships.html#deleting-rows-from-the-many-to-many-table I think this fits good on the most use cases and I am grateful for not keeping an eye on deleting the entries in the association table too. But in my use case (and I think it is not so uncommon) I would like to able to tell SQLAlchemy not to be too clever and not to delete the entries in the association table. This way the database can to its work and check for its integrity, and raising an exception. However, solving the problem on python side is an option so thanks for the hint to the before_flush event. What about using an Association Object as a workaround? Is this also effected from the automatic deletion of the entries in the association table? Torsten -- 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/d/optout.