From: Mike Bayer Date: Thu, 20 Oct 2016 21:36:59 +0000 (-0400) Subject: Don't set pg autoincrement if type affinity is not Integer X-Git-Tag: rel_1_1_3~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=232eec47d13974e9c7bc7bafacba93ddbd59747d;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Don't set pg autoincrement if type affinity is not Integer 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 --- diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst index 6dd09c4d98..4361ee63c8 100644 --- a/doc/build/changelog/changelog_11.rst +++ b/doc/build/changelog/changelog_11.rst @@ -21,6 +21,18 @@ .. 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 diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 85f82ec602..9898e4ba4e 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -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: diff --git a/test/dialect/postgresql/test_reflection.py b/test/dialect/postgresql/test_reflection.py index 84aeef130b..5f9e6df9aa 100644 --- a/test/dialect/postgresql/test_reflection.py +++ b/test/dialect/postgresql/test_reflection.py @@ -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