You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 14, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on May 14, 2026. It is now read-only.
get_multi_foreign_keys Returns Columns in Composite Foreign Key Out of Order #779
CREATE TABLE composite_fk (
a STRING(MAX),
b STRING(MAX),
c STRING(MAX),
CONSTRAINT composite_fk FOREIGN KEY (a, b) REFERENCES composite_pk(a, b),
) PRIMARY KEY (a, b, c);
get_multi_foreign_keys returns referred_columns and constrained_columns in different orders.
I see {"referred_columns": ["b", "a"], "constrained_columns": ["a", "b"]}
This in turn affects alembic auto-generation, because it attempts to drop and recreate the foreign key which appears to have a different order than what I've defined.
#271 reports and #289 offers a fix for the constrained columns, but the referred columns are still effectively unordered.
I think the fundamental problem is that the current SQL used for this relies on the information_schema.constraint_column table for the referred columns, but columns in that table are unordered. They don't have an equivalent for the ordinal_position column in the information_schema.key_column_usage table.
It looks like the intention is that you use the information_schema.referential_constraints table to find the unique constraint associated with the FK's referenced columns and pair it to the referencing columns in the FK itself to get everything in the correct order.
This works, except for one edge case in the emulator. The emulator looks like it incorrectly records information_schema.referential_constraints entries when FKs point across schemas. The referenced table has the wrong schema when the FK is in another schema. So this SQL I think is what we want, but it fails in the emulator (but works with real spanner) for FKs which refer to tables in a different schema:
SELECTtc.table_schema,
tc.table_name,
tc.constraint_name,
tcu.table_name,
tcu.table_schema,
-- Find the corresponding pairs of columns for the foreign key constraint and its related unique constraint.
ARRAY(
SELECT
(kcu.column_name, pkcu.column_name)
FROMinformation_schema.key_column_usageAS kcu
JOINinformation_schema.key_column_usageAS pkcu
ONpkcu.constraint_catalog=rc.unique_constraint_catalogANDpkcu.constraint_schema=rc.unique_constraint_schemaANDpkcu.constraint_name=rc.unique_constraint_nameANDpkcu.ordinal_position=kcu.ordinal_positionWHEREkcu.constraint_catalog=tc.constraint_catalogANDkcu.constraint_schema=tc.constraint_schemaANDkcu.constraint_name=tc.constraint_nameORDER BYkcu.ordinal_position
)
FROMinformation_schema.table_constraintsAS tc
-- Find the foreign key constraint for the referring table.JOINinformation_schema.referential_constraintsAS rc
ONrc.constraint_catalog=tc.constraint_catalogANDrc.constraint_schema=tc.constraint_schemaANDrc.constraint_name=tc.constraint_name-- Find the corresponding unique constraint on the referenced table.JOINinformation_schema.table_constraintsAS tcu
ONtcu.constraint_catalog=rc.unique_constraint_catalogANDtcu.constraint_schema=rc.unique_constraint_schemaANDtcu.constraint_name=rc.unique_constraint_nameJOINinformation_schema.tablesAS t
ONt.table_catalog=tc.table_catalogANDt.table_schema=tc.table_schemaANDt.table_name=tc.table_name
Activity
waltaskew commentedon Oct 15, 2025
I think the fundamental problem is that the current SQL used for this relies on the
information_schema.constraint_columntable for the referred columns, but columns in that table are unordered. They don't have an equivalent for theordinal_positioncolumn in theinformation_schema.key_column_usagetable.It looks like the intention is that you use the
information_schema.referential_constraintstable to find the unique constraint associated with the FK's referenced columns and pair it to the referencing columns in the FK itself to get everything in the correct order.This works, except for one edge case in the emulator. The emulator looks like it incorrectly records
information_schema.referential_constraintsentries when FKs point across schemas. The referenced table has the wrong schema when the FK is in another schema. So this SQL I think is what we want, but it fails in the emulator (but works with real spanner) for FKs which refer to tables in a different schema:fix: Return Correct Column Order in get_multi_foreign_keys
fix: Return Correct Column Order in get_multi_foreign_keys
fix: Return Correct Column Order in get_multi_foreign_keys (#783)