]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fixed reflection of identity columns in tables
authorFederico Caselli <cfederico87@gmail.com>
Thu, 25 Mar 2021 20:08:21 +0000 (21:08 +0100)
committerFederico Caselli <cfederico87@gmail.com>
Thu, 25 Mar 2021 20:08:21 +0000 (21:08 +0100)
with mixed case names in PostgreSQL.

Fixes: #6129
Change-Id: I50480cec03fcb44a668c9b0f9c72950803b771d9

doc/build/changelog/unreleased_14/6129.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/postgresql/test_reflection.py

diff --git a/doc/build/changelog/unreleased_14/6129.rst b/doc/build/changelog/unreleased_14/6129.rst
new file mode 100644 (file)
index 0000000..458f4a6
--- /dev/null
@@ -0,0 +1,6 @@
+.. change::
+    :tags: bug, reflection, postgresql
+    :tickets: 6129
+
+    Fixed reflection of identity columns in tables with mixed case names
+    in PostgreSQL.
\ No newline at end of file
index d52cc2ae6fead519bfa69b69d9db0329ec963774..6e6f5513d01a8fd932bbeb8b3bac5ced4de860b5 100644 (file)
@@ -3582,12 +3582,11 @@ class PGDialect(default.DefaultDialect):
                     'cycle', s.seqcycle)
                 FROM pg_catalog.pg_sequence s
                 JOIN pg_catalog.pg_class c on s.seqrelid = c."oid"
-                JOIN pg_catalog.pg_namespace n on n.oid = c.relnamespace
                 WHERE c.relkind = 'S'
                 AND a.attidentity != ''
-                AND n.nspname || '.' || c.relname =
-                pg_catalog.pg_get_serial_sequence(
-                    a.attrelid::regclass::text, a.attname)
+                AND s.seqrelid = pg_catalog.pg_get_serial_sequence(
+                    a.attrelid::regclass::text, a.attname
+                )::regclass::oid
                 ) as identity_options\
                 """
         else:
index 6586a8308d9cbc859ee6ab7ab10def39cc4dadab..85546491649a8d9f4df70808500e59aefb75d276 100644 (file)
@@ -1811,30 +1811,34 @@ class IdentityReflectionTest(fixtures.TablesTest):
     __backend__ = True
     __requires__ = ("identity_columns",)
 
+    _names = ("t1", "T2", "MiXeDCaSe!")
+
     @classmethod
     def define_tables(cls, metadata):
-        Table(
-            "t1",
-            metadata,
-            Column(
-                "id1",
-                Integer,
-                Identity(
-                    always=True,
-                    start=2,
-                    increment=3,
-                    minvalue=-2,
-                    maxvalue=42,
-                    cycle=True,
-                    cache=4,
+        for name in cls._names:
+            Table(
+                name,
+                metadata,
+                Column(
+                    "id1",
+                    Integer,
+                    Identity(
+                        always=True,
+                        start=2,
+                        increment=3,
+                        minvalue=-2,
+                        maxvalue=42,
+                        cycle=True,
+                        cache=4,
+                    ),
                 ),
-            ),
-            Column("id2", Integer, Identity()),
-            Column("id3", BigInteger, Identity()),
-            Column("id4", SmallInteger, Identity()),
-        )
+                Column("id2", Integer, Identity()),
+                Column("id3", BigInteger, Identity()),
+                Column("id4", SmallInteger, Identity()),
+            )
 
-    def test_reflect_identity(self, connection):
+    @testing.combinations(*_names, argnames="name")
+    def test_reflect_identity(self, connection, name):
         insp = inspect(connection)
         default = dict(
             always=False,
@@ -1844,7 +1848,7 @@ class IdentityReflectionTest(fixtures.TablesTest):
             cycle=False,
             cache=1,
         )
-        cols = insp.get_columns("t1")
+        cols = insp.get_columns(name)
         for col in cols:
             if col["name"] == "id1":
                 is_true("identity" in col)