]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
make qtoken for PostgreSQL _fk_regex_pattern less restrictive
authorGord Thompson <gord@gordthompson.com>
Mon, 19 Jan 2026 12:34:28 +0000 (05:34 -0700)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 21 Jan 2026 01:17:29 +0000 (20:17 -0500)
Improved the foreign key reflection regular expression pattern used by the
PostgreSQL dialect to be more permissive in matching identifier characters,
allowing it to correctly handle unicode characters in table and column
names. This change improves compatibility with PostgreSQL variants such as
CockroachDB that may use different quoting patterns in combination with
unicode characters in their identifiers.  Pull request courtesy Gord
Thompson.

Change-Id: Iaee340879400e01df2f776417e8b1018f1801cfe
(cherry picked from commit d0d9f1b71115471b0a6918075383c2bddf2212b1)

doc/build/changelog/unreleased_20/pg_fk_reflect.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_20/pg_fk_reflect.rst b/doc/build/changelog/unreleased_20/pg_fk_reflect.rst
new file mode 100644 (file)
index 0000000..02db2cc
--- /dev/null
@@ -0,0 +1,10 @@
+.. change::
+    :tags: bug, postgresql
+
+    Improved the foreign key reflection regular expression pattern used by the
+    PostgreSQL dialect to be more permissive in matching identifier characters,
+    allowing it to correctly handle unicode characters in table and column
+    names. This change improves compatibility with PostgreSQL variants such as
+    CockroachDB that may use different quoting patterns in combination with
+    unicode characters in their identifiers.  Pull request courtesy Gord
+    Thompson.
index 77f25142c7c92c5eac310b3177701e4e0f5f3fb0..9904c6d467592c3c879697e36fb56de7c1f14ad5 100644 (file)
@@ -4499,7 +4499,7 @@ class PGDialect(default.DefaultDialect):
     @util.memoized_property
     def _fk_regex_pattern(self):
         # optionally quoted token
-        qtoken = '(?:"[^"]+"|[A-Za-z0-9_]+?)'
+        qtoken = r'(?:"[^"]+"|[\w]+?)'
 
         # https://www.postgresql.org/docs/current/static/sql-createtable.html
         return re.compile(
index 4380026e488a5f3fca6842ca950d1bf52cb9cf5c..d353cfd3357819b289a781249198902177074e4d 100644 (file)
@@ -980,6 +980,36 @@ class ReflectionTest(
             )
         )
 
+    @testing.combinations(
+        "FOREIGN KEY (tid) REFERENCES some_table(id)",
+        'FOREIGN KEY ("tid") REFERENCES some_table(id)',
+        'FOREIGN KEY (tid) REFERENCES "some_table"(id)',
+        'FOREIGN KEY ("測試") REFERENCES unitable1("méil")',
+        "FOREIGN KEY (測試) REFERENCES unitable1(méil)",
+        'FOREIGN KEY ("col_名前") REFERENCES "table_テーブル"("ref_參考")',
+        "FOREIGN KEY (普通_column) REFERENCES 普通_table(普通_ref)",
+        (
+            'FOREIGN KEY ("tid1", tid2) '
+            'REFERENCES some_schema.some_table("id1", id2)'
+        ),
+        (
+            'FOREIGN KEY ("測試1", 測試2) '
+            'REFERENCES "schema_スキーマ"."table_表"("ref1_參考", ref2_参考)'
+        ),
+    )
+    def test_fk_regex_unicode_patterns(self, condef):
+        """Test that FK regex pattern handles unicode identifiers.
+
+        This specifically tests the improved qtoken regex pattern to
+        support PostgreSQL variants such as CockroachDB that deliver
+        unquoted unicode identifiers in FK constraint definitions, whereas
+        PostgreSQL itself delivers the same identifiers with quotes.
+
+        """
+        FK_REGEX = postgresql.dialect()._fk_regex_pattern
+        match = re.search(FK_REGEX, condef)
+        self.assert_(match is not None, f"Expected regex to match: {condef}")
+
     def test_reflect_default_over_128_chars(self, metadata, connection):
         Table(
             "t",