]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- [bug] When the primary key column of a Table
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 23 Sep 2012 16:51:24 +0000 (12:51 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 23 Sep 2012 16:51:24 +0000 (12:51 -0400)
    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]

CHANGES
lib/sqlalchemy/schema.py
lib/sqlalchemy/util/langhelpers.py
test/sql/test_metadata.py

diff --git a/CHANGES b/CHANGES
index 590ac62a9ddbc954826a3dfcf12238d6c8ed7c86..ee5f368dd1a48b43b0b2a7961f21be2da9de8150 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -390,6 +390,12 @@ CHANGES
     returned by SQLite cursor.description.
     [ticket:2475]
 
+  - [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]
 
 0.7.6
 =====
index 02f62b6746a8d36834018930c00eb8120320b83e..b8c74c53cc8901296d4d5544d3f4f670fc2579b6 100644 (file)
@@ -456,14 +456,13 @@ class Table(SchemaItem, expression.TableClause):
     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
@@ -1029,6 +1028,7 @@ class Column(SchemaItem, expression.ColumnClause):
 
         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 "
index 94fbd5eb8e7a2c16b320281820547b67891715d3..a33d4358e4712f1838bb44e0ee73e87db28b6f37 100644 (file)
@@ -494,6 +494,8 @@ class memoized_property(object):
         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.
index 44daf554e12c0ad29f41806ee148b58d3fd3451b..7e77adf5c38a2a916c5f254ff102373c9daddc68 100644 (file)
@@ -13,7 +13,7 @@ import sqlalchemy as tsa
 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):
@@ -720,6 +720,21 @@ class TableTest(fixtures.TestBase, AssertsCompiledSQL):
             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):