Commits

Mike Bayer committed d9c4b40

- [bug] When the primary key column of a Table
is replaced, such as via extend_existing,
the "auto increment" column used by insert()
constructs is reset. Previously it would
remain referring to the previous primary
key column. [ticket:2525]

Comments (0)

Files changed (4)

     operators are present as methods like all
     the other operators.  [ticket:2544]
 
+  - [bug] When the primary key column of a Table
+    is replaced, such as via extend_existing,
+    the "auto increment" column used by insert()
+    constructs is reset.  Previously it would
+    remain referring to the previous primary
+    key column.  [ticket:2525]
 
 - engine
   - [bug] Fixed bug whereby

lib/sqlalchemy/schema.py

     def _init_collections(self):
         pass
 
-
     @util.memoized_property
     def _autoincrement_column(self):
         for col in self.primary_key:
             if col.autoincrement and \
                 col.type._type_affinity is not None and \
                 issubclass(col.type._type_affinity, sqltypes.Integer) and \
-                (not col.foreign_keys or col.autoincrement=='ignore_fk') and \
+                (not col.foreign_keys or col.autoincrement == 'ignore_fk') and \
                 isinstance(col.default, (type(None), Sequence)) and \
                 (col.server_default is None or col.server_default.reflected):
                 return col
 
         if self.primary_key:
             table.primary_key._replace(self)
+            Table._autoincrement_column._reset(table)
         elif self.key in table.primary_key:
             raise exc.ArgumentError(
                 "Trying to redefine primary-key column '%s' as a "
                     "The 'index' keyword argument on Column is boolean only. "
                     "To create indexes with a specific name, create an "
                     "explicit Index object external to the Table.")
-            Index(expression._truncated_label('ix_%s' % self._label), self, unique=self.unique)
+            Index(expression._truncated_label('ix_%s' % self._label),
+                                    self, unique=self.unique)
         elif self.unique:
             if isinstance(self.unique, basestring):
                 raise exc.ArgumentError(
         c = self._constructor(
                 name=self.name,
                 type_=self.type,
-                key = self.key,
-                primary_key = self.primary_key,
-                nullable = self.nullable,
-                unique = self.unique,
+                key=self.key,
+                primary_key=self.primary_key,
+                nullable=self.nullable,
+                unique=self.unique,
                 quote=self.quote,
                 index=self.index,
                 autoincrement=self.autoincrement,
             c = self._constructor(
                 expression._as_truncated(name or self.name),
                 self.type,
-                key = key if key else name if name else self.key,
-                primary_key = self.primary_key,
-                nullable = self.nullable,
-                quote=self.quote, _proxies=[self], *fk)
+                key=key if key else name if name else self.key,
+                primary_key=self.primary_key,
+                nullable=self.nullable,
+                quote=self.quote,
+                _proxies=[self], *fk)
         except TypeError, e:
             # Py3K
             #raise TypeError(

lib/sqlalchemy/util/langhelpers.py

         obj.__dict__[self.__name__] = result = self.fget(obj)
         return result
 
+    def _reset(self, obj):
+        obj.__dict__.pop(self.__name__, None)
 
 class memoized_instancemethod(object):
     """Decorate a method memoize its return value.

test/sql/test_metadata.py

 from test.lib import fixtures
 from test.lib import testing
 from test.lib.testing import ComparesTables, AssertsCompiledSQL
-from test.lib.testing import eq_
+from test.lib.testing import eq_, is_
 
 class MetaDataTest(fixtures.TestBase, ComparesTables):
     def test_metadata_connect(self):
             assign2
         )
 
+    def test_autoincrement_replace(self):
+        m = MetaData()
+
+        t = Table('t', m,
+            Column('id', Integer, primary_key=True)
+        )
+
+        is_(t._autoincrement_column, t.c.id)
+
+        t = Table('t', m,
+            Column('id', Integer, primary_key=True),
+            extend_existing=True
+        )
+        is_(t._autoincrement_column, t.c.id)
+
 class SchemaTest(fixtures.TestBase, AssertsCompiledSQL):
 
     def test_default_schema_metadata_fk(self):