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
for n in remote_cols],
name=name,
onupdate=onupdate,
- ondelete=ondelete
+ ondelete=ondelete,
+ deferrable=deferrable
)
t1.append_constraint(f)
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.
: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.
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)
)
"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"])