- changed milestone to 1.0
- changed component to ext
- changed title to set cascades on relationships based on foreign key
- marked as enhancement
set cascades on relationships based on foreign key
Issue #3210
resolved
I'm use automap_base() to reflect existing MySQL database into the ORM.
There are some Foreign Key constraints which intended to cascade deletions, but the child rows are not deleted.
If we add an NOT NULL constraint on these rows, I got Integrity violation error, because SQLAlchemy try to set IDs to NULL instead of delete them : "UPDATE gestion_horaire SET idscenario=%s WHERE gestion_horaire.id = %s' (None, 41L)"
Comments (3)
-
repo owner -
repo owner - changed status to resolved
- The :mod:
sqlalchemy.ext.automap
extension will now setcascade="all, delete-orphan"
automatically on a one-to-many relationship/backref where the foreign key is detected as containing one or more non-nullable columns. This argument is present in the keywords passed to :func:.automap.generate_relationship
in this case and can still be overridden. Additionally, if the :class:.ForeignKeyConstraint
specifiesondelete="CASCADE"
for a non-nullable orondelete="SET NULL"
for a nullable set of columns, the argumentpassive_deletes=True
is also added to the relationship. Note that not all backends support reflection of ondelete, but backends that do include Postgresql and MySQL. fixes#3210
→ <<cset 5508388f0325>>
-
reporter Thank you very much for your quick response !
- Log in to comment
this is an enhancement. setting up cascades on relationships right now is documented as something you'd do manually, and this specific use case is the example given: http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/automap.html#custom-relationship-arguments.