Skip to content
This repository was archived by the owner on Mar 13, 2026. It is now read-only.

Commit 9ddee8b

Browse files
committed
bug: backwards-compatible include_table=False when returning
In sqlalchemy 2.0, it's: ```python self._label_select_column(None, c, True, False, {}, include_table=False) ``` In older versions, it's ```python self._label_select_column(None, c, True, False, {'include_table': False}) ``` So instead, forward a flag to `vist_column` which can set this kwarg safely itself.
1 parent d947102 commit 9ddee8b

3 files changed

Lines changed: 24 additions & 12 deletions

File tree

google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,11 @@ def limit_clause(self, select, **kw):
382382
return text
383383

384384
def returning_clause(self, stmt, returning_cols, **kw):
385-
# Set include_table=False because although table names are allowed in
386-
# RETURNING clauses, schema names are not.
385+
# Set the spanner_is_returning flag which is passed to visit_column.
387386
columns = [
388-
self._label_select_column(None, c, True, False, {}, include_table=False)
387+
self._label_select_column(
388+
None, c, True, False, {"spanner_is_returning": True}
389+
)
389390
for c in expression._select_iterables(returning_cols)
390391
]
391392

@@ -413,10 +414,6 @@ def visit_table(self, table, spanner_aliased=False, iscrud=False, **kwargs):
413414
414415
And do similar for UPDATE and DELETE statements.
415416
416-
We don't need to correct INSERT statements, which is fortunate
417-
because INSERT statements actually do not currently result in
418-
calls to `visit_table`.
419-
420417
This closely mirrors the mssql dialect which also avoids
421418
schema-qualified columns in SELECTs, although the behaviour is
422419
currently behind a deprecated 'legacy_schema_aliasing' flag.
@@ -437,7 +434,9 @@ def visit_alias(self, alias, **kw):
437434
kw["spanner_aliased"] = alias.element
438435
return super().visit_alias(alias, **kw)
439436

440-
def visit_column(self, column, add_to_result_map=None, **kw):
437+
def visit_column(
438+
self, column, add_to_result_map=None, spanner_is_returning=False, **kw
439+
):
441440
"""Produces column expressions.
442441
443442
In tandem with visit_table, replaces schema-qualified column
@@ -457,6 +456,21 @@ def visit_column(self, column, add_to_result_map=None, **kw):
457456
)
458457

459458
return super().visit_column(converted, **kw)
459+
if spanner_is_returning:
460+
# Set include_table=False because although table names are
461+
# allowed in RETURNING clauses, schema names are not. We
462+
# can't use the same aliasing trick above that we use with
463+
# other statements, because INSERT statements don't result
464+
# in visit_table calls and INSERT table names can't be
465+
# aliased. Statements like:
466+
#
467+
# INSERT INTO table (id, name)
468+
# SELECT id, name FROM another_table
469+
# THEN RETURN another_table.id
470+
#
471+
# aren't legal, so the columns remain unambiguous when not
472+
# qualified by table name.
473+
kw["include_table"] = False
460474

461475
return super().visit_column(column, add_to_result_map=add_to_result_map, **kw)
462476

test/mockserver_tests/test_auto_increment.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ def test_create_table_with_specific_sequence_kind(self):
125125
def test_insert_row(self):
126126
from test.mockserver_tests.auto_increment_model import Singer
127127

128-
self.add_insert_result(
129-
"INSERT INTO singers (name) VALUES (@a0) THEN RETURN singers.id"
130-
)
128+
self.add_insert_result("INSERT INTO singers (name) VALUES (@a0) THEN RETURN id")
131129
engine = create_engine(
132130
"spanner:///projects/p/instances/i/databases/d",
133131
connect_args={"client": self.client, "pool": FixedSizePool(size=10)},

test/mockserver_tests/test_bit_reversed_sequence.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test_insert_row(self):
110110
add_result(
111111
"INSERT INTO singers (id, name) "
112112
"VALUES ( GET_NEXT_SEQUENCE_VALUE(SEQUENCE singer_id), @a0) "
113-
"THEN RETURN singers.id",
113+
"THEN RETURN id",
114114
result,
115115
)
116116
engine = create_engine(

0 commit comments

Comments
 (0)