]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Support 'deferrable' keyword argument for 'create_foreign_key' operation.
authorPedro Romano <pedro.romano@salesseek.co.uk>
Sun, 25 Aug 2013 11:35:16 +0000 (12:35 +0100)
committerPedro Romano <pedro.romano@salesseek.co.uk>
Sun, 25 Aug 2013 11:35:16 +0000 (12:35 +0100)
alembic/operations.py
tests/test_op.py

index 441e54a2464c37e4fc6d1210a6c7815d8c658dce..270be55b797f9f8e23eb404098ee986bbcaec9d1 100644 (file)
@@ -67,7 +67,8 @@ class Operations(object):
     def _foreign_key_constraint(self, name, source, referent,
                                     local_cols, remote_cols,
                                     onupdate=None, ondelete=None,
-                                    source_schema=None, referent_schema=None):
+                                    deferrable=None, source_schema=None,
+                                    referent_schema=None):
         m = sa_schema.MetaData()
         if source == referent:
             t1_cols = local_cols + remote_cols
@@ -88,7 +89,8 @@ class Operations(object):
                                             for n in remote_cols],
                                             name=name,
                                             onupdate=onupdate,
-                                            ondelete=ondelete
+                                            ondelete=ondelete,
+                                            deferrable=deferrable
                                             )
         t1.append_constraint(f)
 
@@ -444,7 +446,8 @@ class Operations(object):
 
     def create_foreign_key(self, name, source, referent, local_cols,
                            remote_cols, onupdate=None, ondelete=None,
-                           source_schema=None, referent_schema=None):
+                           deferrable=None, source_schema=None,
+                           referent_schema=None):
         """Issue a "create foreign key" instruction using the
         current migration context.
 
@@ -482,6 +485,8 @@ class Operations(object):
         :param ondelete: Optional string. If set, emit ON DELETE <value> when
          issuing DDL for this constraint. Typical values include CASCADE,
          DELETE and RESTRICT.
+        :param deferrable: optional bool. If set, emit DEFERRABLE or NOT
+         DEFERRABLE when issuing DDL for this constraint.
         :param source_schema: Optional schema name of the source table.
         :param referent_schema: Optional schema name of the destination table.
 
@@ -491,7 +496,7 @@ class Operations(object):
                     self._foreign_key_constraint(name, source, referent,
                             local_cols, remote_cols,
                             onupdate=onupdate, ondelete=ondelete,
-                            source_schema=source_schema,
+                            deferrable=deferrable, source_schema=source_schema,
                             referent_schema=referent_schema)
                 )
 
index f1ab2570b727cf44c771895e81aa073b477e9e48..581b60668aada79a4a7205c2d33ef047c771d872 100644 (file)
@@ -386,6 +386,16 @@ def test_add_foreign_key_ondelete():
             "REFERENCES t2 (bat, hoho) ON DELETE CASCADE"
     )
 
+def test_add_foreign_key_deferrable():
+    context = op_fixture()
+    op.create_foreign_key('fk_test', 't1', 't2',
+                    ['foo', 'bar'], ['bat', 'hoho'],
+                    deferrable=True)
+    context.assert_(
+        "ALTER TABLE t1 ADD CONSTRAINT fk_test FOREIGN KEY(foo, bar) "
+            "REFERENCES t2 (bat, hoho) DEFERRABLE"
+    )
+
 def test_add_foreign_key_self_referential():
     context = op_fixture()
     op.create_foreign_key("fk_test", "t1", "t1", ["foo"], ["bar"])