]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Don't set pg autoincrement if type affinity is not Integer
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 20 Oct 2016 21:36:59 +0000 (17:36 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 20 Oct 2016 21:38:22 +0000 (17:38 -0400)
Postgresql table reflection will ensure that the
:paramref:`.Column.autoincrement` flag is set to False when reflecting
a primary key column that is not of an :class:`.Integer` datatype,
even if the default is related to an integer-generating sequence.
This can happen if a column is created as SERIAL and the datatype
is changed.  The autoincrement flag can only be True if the datatype
is of integer affinity in the 1.1 series.

This bug is related to a test failure in downstream sqlalchemy_migrate.

Change-Id: I40260e47e1927a1ac940538408983c943bbdba28
Fixes: #3835
doc/build/changelog/changelog_11.rst
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/postgresql/test_reflection.py

index 6dd09c4d981a40510e9c83080db5def6cec2adc1..4361ee63c8c97c2578006ab229833cf0511c7168 100644 (file)
 .. changelog::
     :version: 1.1.3
 
+    .. change::
+        :tags: bug, postgresql
+        :tickets: 3835
+
+        Postgresql table reflection will ensure that the
+        :paramref:`.Column.autoincrement` flag is set to False when reflecting
+        a primary key column that is not of an :class:`.Integer` datatype,
+        even if the default is related to an integer-generating sequence.
+        This can happen if a column is created as SERIAL and the datatype
+        is changed.  The autoincrement flag can only be True if the datatype
+        is of integer affinity in the 1.1 series.
+
     .. change::
         :tags: bug, sql
         :tickets: 3833
index 85f82ec6026549cd792079c01531ce15baeb786e..9898e4ba4e6da562f070313a5d093709e4ada95e 100644 (file)
@@ -2442,7 +2442,8 @@ class PGDialect(default.DefaultDialect):
         if default is not None:
             match = re.search(r"""(nextval\(')([^']+)('.*$)""", default)
             if match is not None:
-                autoincrement = True
+                if issubclass(coltype._type_affinity, sqltypes.Integer):
+                    autoincrement = True
                 # the default is related to a Sequence
                 sch = schema
                 if '.' not in match.group(2) and sch is not None:
index 84aeef130bced5aab0deb0e63ab22916d9336307..5f9e6df9aa2f748d8a8d16e5985589dfcf84eebd 100644 (file)
@@ -343,6 +343,22 @@ class ReflectionTest(fixtures.TestBase):
         r = t3.insert().execute()
         eq_(r.inserted_primary_key, [2])
 
+    @testing.provide_metadata
+    def test_altered_type_autoincrement_pk_reflection(self):
+        metadata = self.metadata
+        t = Table(
+            't', metadata,
+            Column('id', Integer, primary_key=True),
+            Column('x', Integer)
+        )
+        metadata.create_all()
+        testing.db.connect().execution_options(autocommit=True).\
+            execute('alter table t alter column id type varchar(50)')
+        m2 = MetaData(testing.db)
+        t2 = Table('t', m2, autoload=True)
+        eq_(t2.c.id.autoincrement, False)
+        eq_(t2.c.x.autoincrement, False)
+
     @testing.provide_metadata
     def test_renamed_pk_reflection(self):
         metadata = self.metadata