Torsten Irländer <torsten.irlaen...@googlemail.com> wrote:

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

SQLAlchemy is not capable of “ignoring” a constraint.   For the use case of 
many-to-many relationships, if the relationship is established in the direction 
from the object being deleted towards the dependent rows, it will emit a DELETE 
for those rows in the association table before deleting the object itself.  So 
there is no constraint violation.  

The relationship will only take steps to alter the database if it’s not a “view 
only” relationship.  If you put viewonly=True on the side that you don’t want 
this deletion to occur (referring to relationships/backrefs here; it makes a 
difference which side you put it on), you’ll get the integrity violation when 
the object on the local side of that relationship is deleted, if remote rows 
exist.

There is also an option “passive_deletes” which supports the setting “all”, 
indicating that the remote objects should never be nulled out, however this 
feature only applies to one-to-many/many-to-one right now.  It could be made to 
support the “secondary” use case as well, in that it would prevent the UOW from 
ever deleting any rows in the association table, but this feature is not 
implemented right now.

If viewonly is not an option, then I’d recommend using events to check for this 
condition.   


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

Reply via email to