]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Postgres: Do not prefix table with schema in: "FOR UPDATE of <table>"
authorDiana Clarke <diana.joan.clarke@gmail.com>
Tue, 24 Nov 2015 18:41:07 +0000 (13:41 -0500)
committerDiana Clarke <diana.joan.clarke@gmail.com>
Tue, 24 Nov 2015 18:58:50 +0000 (13:58 -0500)
For example, this query:

    SELECT s1.users.name FROM s1.users FOR UPDATE OF s1.users

should actually be:

    SELECT s1.users.name FROM s1.users FOR UPDATE OF users

fixes #3573

lib/sqlalchemy/dialects/postgresql/base.py
lib/sqlalchemy/sql/compiler.py
test/dialect/postgresql/test_compiler.py

index 8de5bede7840312118cf1215e9dcf55c345080ff..e9001f79a0169e648a9706e9242d1149d474d848 100644 (file)
@@ -1158,7 +1158,7 @@ class PGCompiler(compiler.SQLCompiler):
                 c.table if isinstance(c, expression.ColumnClause)
                 else c for c in select._for_update_arg.of)
             tmp += " OF " + ", ".join(
-                self.process(table, ashint=True, **kw)
+                self.process(table, ashint=True, use_schema=False, **kw)
                 for table in tables
             )
 
index f1220ce31fc72471925abaa4575c73c6f17af73d..d28ac313b271b940e05079dedf16c3852a3f91bb 100644 (file)
@@ -1799,9 +1799,9 @@ class SQLCompiler(Compiled):
         return text
 
     def visit_table(self, table, asfrom=False, iscrud=False, ashint=False,
-                    fromhints=None, **kwargs):
+                    fromhints=None, use_schema=True, **kwargs):
         if asfrom or ashint:
-            if getattr(table, "schema", None):
+            if use_schema and getattr(table, "schema", None):
                 ret = self.preparer.quote_schema(table.schema) + \
                     "." + self.preparer.quote(table.name)
             else:
index 78217bd8255c2abb45e4791b32b47516f516f3d5..0c0f9c589c2063939c9ef8841630734e36c7f442 100644 (file)
@@ -580,6 +580,22 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "WHERE mytable_1.myid = %(myid_1)s FOR UPDATE OF mytable_1"
         )
 
+    def test_for_update_with_schema(self):
+        m = MetaData()
+        table1 = Table(
+            'mytable', m,
+            Column('myid'),
+            Column('name'),
+            schema='testschema'
+        )
+
+        self.assert_compile(
+            table1.select(table1.c.myid == 7).with_for_update(of=table1),
+            "SELECT testschema.mytable.myid, testschema.mytable.name "
+            "FROM testschema.mytable "
+            "WHERE testschema.mytable.myid = %(myid_1)s "
+            "FOR UPDATE OF mytable")
+
     def test_reserved_words(self):
         table = Table("pg_table", MetaData(),
                       Column("col1", Integer),