diff options
-rw-r--r-- | alembic/operations.py | 13 | ||||
-rw-r--r-- | tests/test_op.py | 10 |
2 files changed, 19 insertions, 4 deletions
diff --git a/alembic/operations.py b/alembic/operations.py index 441e54a..270be55 100644 --- a/alembic/operations.py +++ b/alembic/operations.py @@ -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) ) diff --git a/tests/test_op.py b/tests/test_op.py index f1ab257..581b606 100644 --- a/tests/test_op.py +++ b/tests/test_op.py @@ -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"]) |