set cascades on relationships based on foreign key

Issue #3210 resolved
yoch created an issue

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)

  1. Mike Bayer repo owner
    • The :mod:sqlalchemy.ext.automap extension will now set cascade="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 specifies ondelete="CASCADE" for a non-nullable or ondelete="SET NULL" for a nullable set of columns, the argument passive_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>>

  2. Log in to comment