- [bug] Repaired create_foreign_key() for

self-referential foreign keys, which weren't working
  at all.
This commit is contained in:
Mike Bayer
2012-08-04 07:18:01 -04:00
parent 156e79f918
commit 7ee8f96b6f
3 changed files with 20 additions and 3 deletions

View File

@@ -11,6 +11,10 @@
config option is being used but SQL access isn't
desired.
- [bug] Repaired create_foreign_key() for
self-referential foreign keys, which weren't working
at all.
- [bug] 'alembic' command reports an informative
error message when the configuration is missing
the 'script_directory' key. #63

View File

@@ -54,11 +54,16 @@ class Operations(object):
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])
t2 = schema.Table(referent, m,
if source == referent:
t1_cols = local_cols + remote_cols
else:
t1_cols = local_cols
schema.Table(referent, m,
*[schema.Column(n, NULLTYPE) for n in remote_cols])
t1 = schema.Table(source, m,
*[schema.Column(n, NULLTYPE) for n in t1_cols])
f = schema.ForeignKeyConstraint(local_cols,
["%s.%s" % (referent, n)
for n in remote_cols],

View File

@@ -196,6 +196,14 @@ def test_add_foreign_key_ondelete():
"REFERENCES t2 (bat, hoho) ON DELETE CASCADE"
)
def test_add_foreign_key_self_referential():
context = op_fixture()
op.create_foreign_key("fk_test", "t1", "t1", ["foo"], ["bar"])
context.assert_(
"ALTER TABLE t1 ADD CONSTRAINT fk_test "
"FOREIGN KEY(foo) REFERENCES t1 (bar)"
)
def test_add_check_constraint():
context = op_fixture()
op.create_check_constraint(