From: Mike Bayer Date: Wed, 5 Oct 2011 15:54:15 +0000 (-0400) Subject: - Fixed bug related to [ticket:2141] whereby the X-Git-Tag: rel_0_6_9~22 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fa99325dd36c0d5c9c3ec4af071fd83a6eb8397a;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Fixed bug related to [ticket:2141] whereby the same modified index behavior in PG 9 affected primary key reflection on a renamed column. [ticket:2291]. --- diff --git a/CHANGES b/CHANGES index e7f3992612..7cb3694e9b 100644 --- a/CHANGES +++ b/CHANGES @@ -84,6 +84,12 @@ CHANGES ForeignKeyConstraint refers to a column name in the parent that is not found. +- postgresql + - Fixed bug related to [ticket:2141] whereby the + same modified index behavior in PG 9 affected + primary key reflection on a renamed column. + [ticket:2291]. + - mysql - Fixed OurSQL dialect to use ansi-neutral quote symbol "'" for XA commands instead diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 465bfe0ea4..aa023acfbf 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -1230,13 +1230,19 @@ class PGDialect(default.DefaultDialect): def get_primary_keys(self, connection, table_name, schema=None, **kw): table_oid = self.get_table_oid(connection, table_name, schema, info_cache=kw.get('info_cache')) + PK_SQL = """ - SELECT attname FROM pg_attribute - WHERE attrelid = ( - SELECT indexrelid FROM pg_index i - WHERE i.indrelid = :table_oid - AND i.indisprimary = 't') - ORDER BY attnum + SELECT a.attname + FROM + pg_class t + join pg_index ix on t.oid = ix.indrelid + join pg_attribute a + on t.oid=a.attrelid and a.attnum=ANY(ix.indkey) + WHERE + t.oid = :table_oid and + ix.indisprimary = 't' + ORDER BY + a.attnum """ t = sql.text(PK_SQL, typemap={'attname':sqltypes.Unicode}) c = connection.execute(t, table_oid=table_oid) diff --git a/test/dialect/test_postgresql.py b/test/dialect/test_postgresql.py index 9287229934..ae05dda6e4 100644 --- a/test/dialect/test_postgresql.py +++ b/test/dialect/test_postgresql.py @@ -477,6 +477,16 @@ class EnumTest(TestBase, AssertsExecutionResults, AssertsCompiledSQL): finally: metadata.drop_all() + @testing.provide_metadata + def test_renamed_pk_reflection(self): + t = Table('t', metadata, Column('id', Integer, primary_key=True)) + metadata.create_all() + testing.db.connect().execution_options(autocommit=True).\ + execute('alter table t rename id to t_id') + m2 = MetaData(testing.db) + t2 = Table('t', m2, autoload=True) + eq_([c.name for c in t2.primary_key], ['t_id']) + def test_schema_reflection(self): metadata = MetaData(testing.db) etype = Enum(