Commits

Mike Bayer  committed a41b090

Fixed bug where autogenerate would fail if a Column
to be added to a table made use of the ".key" paramter.
#99

  • Participants
  • Parent commits b3bc2d8

Comments (0)

Files changed (3)

File alembic/autogenerate.py

 
 def _compare_tables(conn_table_names, metadata_table_names,
                     inspector, metadata, diffs, autogen_context):
+
     for s, tname in metadata_table_names.difference(conn_table_names):
         name = '%s.%s' % (s, tname) if s else tname
         diffs.append(("add_table", metadata.tables[name]))
         log.info("Detected removed column '%s.%s'", name, cname)
 
     for colname in metadata_col_names.intersection(conn_col_names):
-        metadata_col = metadata_table.c[colname]
+        metadata_col = metadata_cols_by_name[colname]
         conn_col = conn_table[colname]
         col_diff = []
         _compare_type(schema, tname, colname,

File docs/build/changelog.rst

     :version: 0.4.2
 
     .. change::
+        :tags: bug, autogenerate
+        :tickets: 99
+
+      Fixed bug where autogenerate would fail if a Column
+      to be added to a table made use of the ".key" paramter.
+
+    .. change::
         :tags: bug, sqlite
         :tickets: 98
 

File tests/test_autogenerate.py

             [('remove_table', 'extra'), ('remove_table', u'user')]
         )
 
+class AutogenKeyTest(AutogenTest, TestCase):
+    @classmethod
+    def _get_db_schema(cls):
+        m = MetaData()
+
+        Table('someothertable', m,
+            Column('id', Integer, primary_key=True),
+            Column('value', Integer, key="somekey"),
+        )
+        return m
+
+    @classmethod
+    def _get_model_schema(cls):
+        m = MetaData()
+
+        Table('sometable', m,
+            Column('id', Integer, primary_key=True),
+            Column('value', Integer, key="someotherkey"),
+        )
+        Table('someothertable', m,
+            Column('id', Integer, primary_key=True),
+            Column('value', Integer, key="somekey"),
+            Column("othervalue", Integer, key="otherkey")
+        )
+        return m
+
+    symbols = ['someothertable', 'sometable']
+    def test_autogen(self):
+        metadata = self.m2
+        connection = self.context.bind
+
+        diffs = []
+
+        autogenerate._produce_net_changes(connection, metadata, diffs,
+                                          self.autogen_context,
+                                          include_schemas=False
+                                          )
+        eq_(diffs[0][0], "add_table")
+        eq_(diffs[0][1].name, "sometable")
+        eq_(diffs[1][0], "add_column")
+        eq_(diffs[1][3].key, "otherkey")
+
 class AutogenerateDiffOrderTest(TestCase):
     @classmethod
     @requires_07