diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-10-20 17:36:59 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-10-20 17:38:22 -0400 |
commit | 232eec47d13974e9c7bc7bafacba93ddbd59747d (patch) | |
tree | be24dca93d7ea036208a55663b90174de106c181 | |
parent | 78f2864b90c9df03aaa2fdddb9fd7d394abdbc31 (diff) | |
download | sqlalchemy-232eec47d13974e9c7bc7bafacba93ddbd59747d.tar.gz |
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
-rw-r--r-- | doc/build/changelog/changelog_11.rst | 12 | ||||
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/base.py | 3 | ||||
-rw-r--r-- | test/dialect/postgresql/test_reflection.py | 16 |
3 files changed, 30 insertions, 1 deletions
diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst index 6dd09c4d9..4361ee63c 100644 --- a/doc/build/changelog/changelog_11.rst +++ b/doc/build/changelog/changelog_11.rst @@ -22,6 +22,18 @@ :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 85f82ec60..9898e4ba4 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 84aeef130..5f9e6df9a 100644 --- a/test/dialect/postgresql/test_reflection.py +++ b/test/dialect/postgresql/test_reflection.py @@ -344,6 +344,22 @@ class ReflectionTest(fixtures.TestBase): 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 t = Table('t', metadata, Column('id', Integer, primary_key=True)) |