]> 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:23 +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

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 d12ae61716ac16bfe33ebd872c5fd1fbf67b7731..7b31b0c0c41c104d552b044e76f710c023cbfe60 100644 (file)
@@ -4819,7 +4819,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 4ede0f6a2e67ea91c495bca7aa72f1c8336e485a..1366cb7a32e33cd8bc60e129c183c07a2fcab398 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",