]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Ensure schema names also compared in foreign_key_constraint()
authorKonstantin Lebedev <klebedev@blondinka.ru>
Mon, 20 Feb 2017 16:28:06 +0000 (11:28 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 20 Feb 2017 16:41:58 +0000 (11:41 -0500)
Fixed bug in :func:`.ops.create_foreign_key` where the internal table
representation would not be created properly if the foriegn key referred
to a table in a different schema of the same name.  Pull request
courtesy Konstantin Lebedev.

Change-Id: I494c95d02aedbfec3b6318d069322b544cf018fb
Pull-request: https://github.com/zzzeek/alembic/pull/36

alembic/operations/schemaobj.py
docs/build/changelog.rst
tests/test_op.py

index f0f81057f4ed5b5dd76167000d01c71ef518d1f6..a01f5be286615118aaa6da701d3cc4672547b639 100644 (file)
@@ -29,7 +29,7 @@ class SchemaObjects(object):
         referent_schema=None, initially=None,
             match=None, **dialect_kw):
         m = self.metadata()
-        if source == referent:
+        if source == referent and source_schema == referent_schema:
             t1_cols = local_cols + remote_cols
         else:
             t1_cols = local_cols
index 71efc830fa6a2cd4d94cf871a2e74b5d9a67e4bb..01a23a7b2d5f5c1cca4dad56f697339c8bba99cd 100644 (file)
@@ -7,6 +7,14 @@ Changelog
     :version: 0.8.11
     :released:
 
+    .. change:: fk_schema_compare
+      :tags: bug, operations
+
+      Fixed bug in :func:`.ops.create_foreign_key` where the internal table
+      representation would not be created properly if the foriegn key referred
+      to a table in a different schema of the same name.  Pull request
+      courtesy Konstantin Lebedev.
+
 .. changelog::
     :version: 0.8.10
     :released: January 17, 2017
index a4ada7bd91abecf3ee0e2f7acf2a9846752bf1c8..0c0608a267e86c895a0e36041bd4ad3d1c2e8bb2 100644 (file)
@@ -438,6 +438,16 @@ class OpTest(TestBase):
             "REFERENCES bar2.t2 (bat, hoho)"
         )
 
+    def test_add_foreign_key_schema_same_tablename(self):
+        context = op_fixture()
+        op.create_foreign_key('fk_test', 't1', 't1',
+                              ['foo', 'bar'], ['bat', 'hoho'],
+                              source_schema='foo2', referent_schema='bar2')
+        context.assert_(
+            "ALTER TABLE foo2.t1 ADD CONSTRAINT fk_test FOREIGN KEY(foo, bar) "
+            "REFERENCES bar2.t1 (bat, hoho)"
+        )
+
     def test_add_foreign_key_onupdate(self):
         context = op_fixture()
         op.create_foreign_key('fk_test', 't1', 't2',