]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug related to [ticket:2141] whereby the
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 5 Oct 2011 15:54:15 +0000 (11:54 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 5 Oct 2011 15:54:15 +0000 (11:54 -0400)
    same modified index behavior in PG 9 affected
    primary key reflection on a renamed column.
    [ticket:2291].

CHANGES
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/test_postgresql.py

diff --git a/CHANGES b/CHANGES
index e7f39926121ecf85cca519105b4c0354b4a2b97d..7cb3694e9b351b638a15ac855269019f3ddb84d7 100644 (file)
--- 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
index 465bfe0ea42e01a925dfa76924afa45e221a99e9..aa023acfbf39335698e06120ce57e926e0e0f27c 100644 (file)
@@ -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)
index 928722993465fb64d8c86b0518857f817f3a730c..ae05dda6e495cd22585c34d6be87a88337362d98 100644 (file)
@@ -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(