From 86b8dd7d8edfcaf7411b5bd2ab5ba89d298d0a16 Mon Sep 17 00:00:00 2001 From: Sebastian Cao Date: Sun, 5 Jul 2026 09:47:43 +0800 Subject: [PATCH] Strip PostgreSQL type cast specifiers when comparing computed defaults --- alembic/autogenerate/compare/server_defaults.py | 5 ++++- docs/build/unreleased/1462.rst | 10 ++++++++++ tests/test_postgresql.py | 17 +++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 docs/build/unreleased/1462.rst diff --git a/alembic/autogenerate/compare/server_defaults.py b/alembic/autogenerate/compare/server_defaults.py index 1e09e8e21..b33524066 100644 --- a/alembic/autogenerate/compare/server_defaults.py +++ b/alembic/autogenerate/compare/server_defaults.py @@ -55,7 +55,10 @@ def _normalize_computed_default(sqltext: str) -> str: """ - return re.sub(r"[ \(\)'\"`\[\]\t\r\n]", "", sqltext).lower() + normalized_sqltext = re.sub(r"[ \(\)'\"`\[\]\t\r\n]", "", sqltext).lower() + # strip postgresql type cast specifiers, e.g. ``::regconfig``, ``::text``, + # which can appear inconsistently and cause false-positive warnings. + return re.sub(r"::\w+", "", normalized_sqltext) def _compare_computed_default( diff --git a/docs/build/unreleased/1462.rst b/docs/build/unreleased/1462.rst new file mode 100644 index 000000000..fd919066e --- /dev/null +++ b/docs/build/unreleased/1462.rst @@ -0,0 +1,10 @@ +.. change:: + :tags: bug, autogenerate, postgresql + :tickets: 1462 + + Fixed false-positive "Computed default cannot be modified" warning emitted + during autogenerate when comparing PostgreSQL ``Computed`` columns whose + reflected SQL expression includes type cast specifiers such as + ``::regconfig`` or ``::text``. These cast specifiers are now stripped + before comparison so that semantically equivalent expressions no longer + trigger the warning. diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index d0304651d..77e614bf3 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -72,6 +72,9 @@ from alembic.autogenerate.compare.server_defaults import ( _dialect_impl_compare_server_default as _compare_server_default, ) + from alembic.autogenerate.compare.server_defaults import ( + _normalize_computed_default, + ) class PostgresqlOpTest(TestBase): @@ -395,6 +398,20 @@ def test_alter_column_computed_not_supported(self, sd, esd): existing_server_default=esd(), ) + def test_normalize_computed_default_strips_type_casts(self): + # type cast specifiers such as ``::regconfig`` only appear on the + # reflected (connection) side and would otherwise cause a + # false-positive "cannot be modified" warning. issue #1462 + eq_( + _normalize_computed_default( + "setweight(to_tsvector('english', title), 'a')" + ), + _normalize_computed_default( + "setweight(to_tsvector('english'::regconfig, " + "title::text), 'a'::\"char\")" + ), + ) + @combinations( ({}, None), (dict(always=True), None),