]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
fix inline_references to emit FK referential actions
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 25 Jun 2026 14:53:58 +0000 (10:53 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 25 Jun 2026 15:02:21 +0000 (11:02 -0400)
Fixed bug where :paramref:`.Operations.add_column.inline_references`
did not render ``ON DELETE``, ``ON UPDATE``, ``DEFERRABLE``,
``INITIALLY``, or ``MATCH`` clauses when emitting the inline
``REFERENCES`` clause.

Fixes: #1820
Change-Id: I71c94cd1ba6dec0fcb31fe2958be8bfb8c5c5505

alembic/ddl/base.py
docs/build/unreleased/1820.rst [new file with mode: 0644]
tests/test_op.py

index 30a3a15a39b0011679a041f12c0e2ea313d517e7..b39049499be73c5066f14e20c7df3ce7cbffa08f 100644 (file)
@@ -396,6 +396,9 @@ def add_column(
             table_name,
             compiler.preparer.quote(ref_col.name),
         )
+        text += compiler.define_constraint_match(fk.constraint)
+        text += compiler.define_constraint_cascades(fk.constraint)
+        text += compiler.define_constraint_deferrability(fk.constraint)
 
     const = " ".join(
         compiler.process(constraint) for constraint in column.constraints
diff --git a/docs/build/unreleased/1820.rst b/docs/build/unreleased/1820.rst
new file mode 100644 (file)
index 0000000..f8f0791
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: bug, operations
+    :tickets: 1820
+
+    Fixed bug where the ``inline_references`` parameter of
+    :meth:`.Operations.add_column` did not include foreign key referential
+    actions such as ``ON DELETE``, ``ON UPDATE``, ``DEFERRABLE``,
+    ``INITIALLY``, and ``MATCH`` when rendering the inline ``REFERENCES``
+    clause.
index 704e2e919321ce89dbd59c60ebca3bd05206656a..e3fbf2535ca176b5e6288f18bb1c036b4e1b3fcc 100644 (file)
@@ -388,6 +388,37 @@ class OpTest(TestBase):
             f"ALTER TABLE t1 ADD COLUMN c1 INTEGER NOT NULL {expected_ref}"
         )
 
+    @combinations(
+        ({"ondelete": "SET NULL"}, "ON DELETE SET NULL"),
+        ({"onupdate": "CASCADE"}, "ON UPDATE CASCADE"),
+        (
+            {"ondelete": "SET NULL", "onupdate": "CASCADE"},
+            "ON DELETE SET NULL ON UPDATE CASCADE",
+        ),
+        (
+            {"deferrable": True, "initially": "DEFERRED"},
+            "DEFERRABLE INITIALLY DEFERRED",
+        ),
+        ({"deferrable": False}, "NOT DEFERRABLE"),
+        ({"match": "FULL"}, "MATCH FULL"),
+        argnames="fk_kwargs,expected_suffix",
+    )
+    def test_add_column_fk_inline_references_referential_actions(
+        self, fk_kwargs, expected_suffix
+    ):
+        context = op_fixture()
+        op.add_column(
+            "t1",
+            Column(
+                "c1", Integer, ForeignKey("c2.id", **fk_kwargs), nullable=True
+            ),
+            inline_references=True,
+        )
+        context.assert_(
+            f"ALTER TABLE t1 ADD COLUMN c1 INTEGER "
+            f"REFERENCES c2 (id) {expected_suffix}"
+        )
+
     def test_add_column_multiple_fk_inline_references(self):
         """Test that inline_references is ignored when a column has multiple
         ForeignKey objects to avoid non-deterministic behavior."""