From: Mike Bayer Date: Tue, 11 Mar 2025 12:33:30 +0000 (-0400) Subject: consolidate kwargs for "FOR UPDATE OF" X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=21630d2574328a0f01a1e994e264f56f1adf99db;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git consolidate kwargs for "FOR UPDATE OF" Fixed compiler issue in the PostgreSQL dialect where incorrect keywords would be passed when using "FOR UPDATE OF" inside of a subquery. Fixes: #12417 Change-Id: I6255d165e8e719e1786e78aa60ee8e6a95af1dcb --- diff --git a/doc/build/changelog/unreleased_20/12417.rst b/doc/build/changelog/unreleased_20/12417.rst new file mode 100644 index 0000000000..b9b22a8247 --- /dev/null +++ b/doc/build/changelog/unreleased_20/12417.rst @@ -0,0 +1,6 @@ +.. change:: + :tags: bug, postgresql + :tickets: 12417 + + Fixed compiler issue in the PostgreSQL dialect where incorrect keywords + would be passed when using "FOR UPDATE OF" inside of a subquery. diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index e640e2a4cd..1f00127bfa 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -2001,9 +2001,10 @@ class PGCompiler(compiler.SQLCompiler): for c in select._for_update_arg.of: tables.update(sql_util.surface_selectables_only(c)) + of_kw = dict(kw) + of_kw.update(ashint=True, use_schema=False) tmp += " OF " + ", ".join( - self.process(table, ashint=True, use_schema=False, **kw) - for table in tables + self.process(table, **of_kw) for table in tables ) if select._for_update_arg.nowait: diff --git a/test/dialect/postgresql/test_compiler.py b/test/dialect/postgresql/test_compiler.py index b6bd625708..8e241b82e5 100644 --- a/test/dialect/postgresql/test_compiler.py +++ b/test/dialect/postgresql/test_compiler.py @@ -1733,6 +1733,15 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): "FOR UPDATE OF table1", ) + # test issue #12417 + subquery = select(table1.c.myid).with_for_update(of=table1).lateral() + statement = select(subquery.c.myid) + self.assert_compile( + statement, + "SELECT anon_1.myid FROM LATERAL (SELECT mytable.myid AS myid " + "FROM mytable FOR UPDATE OF mytable) AS anon_1", + ) + def test_for_update_with_schema(self): m = MetaData() table1 = Table(