Commits

Mike Bayer  committed 4a7c690

Fixed bug where :meth:`.Table.tometadata` would fail if a
:class:`.Column` had both a foreign key as well as an
alternate ".key" name for the column.
[ticket:2643]

  • Participants
  • Parent commits 80ed73f
  • Branches rel_0_7

Comments (0)

Files changed (3)

File doc/build/changelog/changelog_07.rst

     :released:
 
     .. change::
+        :tags: sql, bug
+        :tickets: 2643
+
+        Fixed bug where :meth:`.Table.tometadata` would fail if a
+        :class:`.Column` had both a foreign key as well as an
+        alternate ".key" name for the column.
+
+    .. change::
         :tags: engine, bug
         :tickets: 2604
 

File lib/sqlalchemy/schema.py

 
     def copy(self, **kw):
         fkc = ForeignKeyConstraint(
-                    [x.parent.name for x in self._elements.values()],
+                    [x.parent.key for x in self._elements.values()],
                     [x._get_colspec(**kw) for x in self._elements.values()],
                     name=self.name,
                     onupdate=self.onupdate,

File test/sql/test_metadata.py

         finally:
             meta.drop_all(testing.db)
 
+    def test_col_key_fk_parent_tometadata(self):
+        # test #2643
+        m1 = MetaData()
+        a = Table('a', m1, Column('x', Integer))
+        b = Table('b', m1, Column('x', Integer, ForeignKey('a.x'), key='y'))
+        assert b.c.y.references(a.c.x)
+
+        m2 = MetaData()
+        b2 = b.tometadata(m2)
+        a2 = a.tometadata(m2)
+        assert b2.c.y.references(a2.c.x)
+
     def test_pickle_metadata_sequence_restated(self):
         m1 = MetaData()
         Table('a', m1,