]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Added optional onupdate and ondelete params to Operations.create_check_constraint
authorGiacomo Bagnoli <g.bagnoli@asidev.com>
Wed, 8 Feb 2012 16:58:46 +0000 (17:58 +0100)
committerGiacomo Bagnoli <g.bagnoli@asidev.com>
Wed, 8 Feb 2012 16:58:46 +0000 (17:58 +0100)
alembic/operations.py
tests/test_op.py

index 1bbe5a1a32bcf3e9a1c350fff45251ba7a2e43a9..22f260c6d55e6abfe570f68caa46637ffb4275e9 100644 (file)
@@ -51,7 +51,8 @@ class Operations(object):
         alembic.op._remove_proxy()
 
     def _foreign_key_constraint(self, name, source, referent, 
-                                    local_cols, remote_cols):
+                                    local_cols, remote_cols,
+                                    onupdate=None, ondelete=None):
         m = schema.MetaData()
         t1 = schema.Table(source, m, 
                 *[schema.Column(n, NULLTYPE) for n in local_cols])
@@ -61,7 +62,9 @@ class Operations(object):
         f = schema.ForeignKeyConstraint(local_cols, 
                                             ["%s.%s" % (referent, n) 
                                             for n in remote_cols],
-                                            name=name
+                                            name=name,
+                                            onupdate=onupdate,
+                                            ondelete=ondelete
                                             )
         t1.append_constraint(f)
 
@@ -315,7 +318,7 @@ class Operations(object):
 
 
     def create_foreign_key(self, name, source, referent, local_cols, 
-                                    remote_cols):
+                           remote_cols, onupdate=None, ondelete=None):
         """Issue a "create foreign key" instruction using the 
         current migration context.
 
@@ -349,12 +352,19 @@ class Operations(object):
          source table.
         :param remote_cols: a list of string column names in the
          remote table.
+        :param onupdate: Optional string. If set, emit ON UPDATE <value> when
+         issuing DDL for this constraint. Typical values include CASCADE,
+         DELETE and RESTRICT.
+        :param ondelete: Optional string. If set, emit ON DELETE <value> when
+         issuing DDL for this constraint. Typical values include CASCADE,
+         DELETE and RESTRICT.
 
         """
 
         self.impl.add_constraint(
                     self._foreign_key_constraint(name, source, referent, 
-                            local_cols, remote_cols)
+                            local_cols, remote_cols,
+                            onupdate=onupdate, ondelete=ondelete)
                 )
 
     def create_unique_constraint(self, name, source, local_cols, **kw):
index 8af4711002e7bbee93d42325a0f4de136f77152f..58f19cb8c0c6871fcac0f9ff4ed516a8ea660956 100644 (file)
@@ -154,6 +154,26 @@ def test_add_foreign_key():
             "REFERENCES t2 (bat, hoho)"
     )
 
+def test_add_foreign_key_onupdate():
+    context = op_fixture()
+    op.create_foreign_key('fk_test', 't1', 't2',
+                    ['foo', 'bar'], ['bat', 'hoho'],
+                    onupdate='CASCADE')
+    context.assert_(
+        "ALTER TABLE t1 ADD CONSTRAINT fk_test FOREIGN KEY(foo, bar) "
+            "REFERENCES t2 (bat, hoho) ON UPDATE CASCADE"
+    )
+
+def test_add_foreign_key_ondelete():
+    context = op_fixture()
+    op.create_foreign_key('fk_test', 't1', 't2',
+                    ['foo', 'bar'], ['bat', 'hoho'],
+                    ondelete='CASCADE')
+    context.assert_(
+        "ALTER TABLE t1 ADD CONSTRAINT fk_test FOREIGN KEY(foo, bar) "
+            "REFERENCES t2 (bat, hoho) ON DELETE CASCADE"
+    )
+
 def test_add_check_constraint():
     context = op_fixture()
     op.create_check_constraint(