Skip to content

Commit 16c87e4

Browse files
twoodwarkToby Woodwarkolavloite
authored
fix: column order in get_multi_pk_constraint (#640)
* fix: column order in get_multi_pk_constraint * chore: order by catalog and schema name --------- Co-authored-by: Toby Woodwark <[email protected]> Co-authored-by: Knut Olav Løite <[email protected]>
1 parent 3bfbdc0 commit 16c87e4

File tree

4 files changed

+4
-50
lines changed

4 files changed

+4
-50
lines changed

google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,16 +1304,18 @@ def get_multi_pk_constraint(
13041304
table_type_query = self._get_table_type_query(kind, True)
13051305

13061306
sql = """
1307-
SELECT tc.table_schema, tc.table_name, ccu.COLUMN_NAME
1307+
SELECT tc.table_schema, tc.table_name, kcu.column_name
13081308
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tc
1309-
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS ccu
1309+
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS kcu
13101310
USING (TABLE_CATALOG, TABLE_SCHEMA, CONSTRAINT_NAME)
13111311
JOIN information_schema.tables AS t
13121312
ON tc.TABLE_CATALOG = t.TABLE_CATALOG
13131313
AND tc.TABLE_SCHEMA = t.TABLE_SCHEMA
13141314
AND tc.TABLE_NAME = t.TABLE_NAME
13151315
WHERE {table_filter_query} {table_type_query}
13161316
{schema_filter_query} tc.CONSTRAINT_TYPE = "PRIMARY KEY"
1317+
ORDER BY tc.table_catalog ASC, tc.table_schema ASC,
1318+
tc.table_name ASC, kcu.ordinal_position ASC
13171319
""".format(
13181320
table_filter_query=table_filter_query,
13191321
table_type_query=table_type_query,

test/test_suite_13.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,22 +1014,6 @@ def test_fk_column_order(self):
10141014
eq_(set(fkey1.get("referred_columns")), {"name", "id", "attr"})
10151015
eq_(set(fkey1.get("constrained_columns")), {"pname", "pid", "pattr"})
10161016

1017-
@testing.requires.primary_key_constraint_reflection
1018-
def test_pk_column_order(self, connection):
1019-
"""
1020-
SPANNER OVERRIDE:
1021-
Emultor doesn't support returning pk sorted by ordinal value
1022-
of columns.
1023-
"""
1024-
insp = inspect(connection)
1025-
primary_key = insp.get_pk_constraint(self.tables.tb1.name)
1026-
exp = (
1027-
["id", "name", "attr"]
1028-
if bool(os.environ.get("SPANNER_EMULATOR_HOST"))
1029-
else ["name", "id", "attr"]
1030-
)
1031-
eq_(primary_key.get("constrained_columns"), exp)
1032-
10331017

10341018
class RowFetchTest(_RowFetchTest):
10351019
def test_row_w_scalar_select(self):

test/test_suite_14.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -858,22 +858,6 @@ def test_fk_column_order(self):
858858
eq_(set(fkey1.get("referred_columns")), {"name", "id", "attr"})
859859
eq_(set(fkey1.get("constrained_columns")), {"pname", "pid", "pattr"})
860860

861-
@testing.requires.primary_key_constraint_reflection
862-
def test_pk_column_order(self, connection):
863-
"""
864-
SPANNER OVERRIDE:
865-
Emultor doesn't support returning pk sorted by ordinal value
866-
of columns.
867-
"""
868-
insp = inspect(connection)
869-
primary_key = insp.get_pk_constraint(self.tables.tb1.name)
870-
exp = (
871-
["id", "name", "attr"]
872-
if bool(os.environ.get("SPANNER_EMULATOR_HOST"))
873-
else ["name", "id", "attr"]
874-
)
875-
eq_(primary_key.get("constrained_columns"), exp)
876-
877861

878862
@pytest.mark.skip("Spanner doesn't support quotes in table names.")
879863
class QuotedNameArgumentTest(_QuotedNameArgumentTest):

test/test_suite_20.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,22 +1539,6 @@ def test_fk_column_order(self, connection):
15391539
eq_(set(fkey1.get("referred_columns")), {"name", "id", "attr"})
15401540
eq_(set(fkey1.get("constrained_columns")), {"pname", "pid", "pattr"})
15411541

1542-
@testing.requires.primary_key_constraint_reflection
1543-
def test_pk_column_order(self, connection):
1544-
"""
1545-
SPANNER OVERRIDE:
1546-
Emultor doesn't support returning pk sorted by ordinal value
1547-
of columns.
1548-
"""
1549-
insp = inspect(connection)
1550-
primary_key = insp.get_pk_constraint(self.tables.tb1.name)
1551-
exp = (
1552-
["id", "name", "attr"]
1553-
if bool(os.environ.get("SPANNER_EMULATOR_HOST"))
1554-
else ["name", "id", "attr"]
1555-
)
1556-
eq_(primary_key.get("constrained_columns"), exp)
1557-
15581542

15591543
@pytest.mark.skip("Spanner doesn't support quotes in table names.")
15601544
class QuotedNameArgumentTest(_QuotedNameArgumentTest):

0 commit comments

Comments
 (0)