Skip to content

bug: Support Retrieval of Cross-Schema Foreign Keys #681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,8 +607,17 @@ def visit_drop_table(self, drop_table, **kw):
constrs = ""
for cons in drop_table.element.constraints:
if isinstance(cons, ForeignKeyConstraint) and cons.name:
effective_schema = self.preparer.schema_for_object(drop_table.element)
if effective_schema:
table = (
f"{self.preparer.quote_schema(effective_schema)}"
"."
f"{self.preparer.quote(drop_table.element.name)}"
)
else:
table = self.preparer.quote(drop_table.element.name)
constrs += "ALTER TABLE {table} DROP CONSTRAINT {constr};".format(
table=drop_table.element.name,
table=table,
constr=self.preparer.quote(cons.name),
)

Expand Down Expand Up @@ -1472,10 +1481,12 @@ def get_multi_foreign_keys(
)
FROM information_schema.table_constraints AS tc
JOIN information_schema.constraint_column_usage AS ccu
USING (table_catalog, table_schema, constraint_name)
ON ccu.table_catalog = tc.table_catalog
and ccu.constraint_schema = tc.table_schema
and ccu.constraint_name = tc.constraint_name
JOIN information_schema.constraint_table_usage AS ctu
ON ctu.table_catalog = tc.table_catalog
and ctu.table_schema = tc.table_schema
and ctu.constraint_schema = tc.table_schema
and ctu.constraint_name = tc.constraint_name
JOIN information_schema.key_column_usage AS kcu
ON kcu.table_catalog = tc.table_catalog
Expand Down
64 changes: 63 additions & 1 deletion test/system/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
Table,
Column,
Integer,
ForeignKey,
PrimaryKeyConstraint,
String,
Index,
MetaData,
Boolean,
BIGINT,
inspect,
select,
update,
delete,
Expand Down Expand Up @@ -59,6 +61,16 @@ def define_tables(cls, metadata):
Column("ID", Integer, primary_key=True),
Column("name", String(20)),
)
# Add a foreign key example.
Table(
"number_colors",
metadata,
Column("ID", Integer, primary_key=True),
Column(
"number_id", Integer, ForeignKey("numbers.number", name="number_fk")
),
Column("color", String(20)),
)

with cls.bind.begin() as conn:
conn.execute(text("CREATE SCHEMA IF NOT EXISTS schema"))
Expand All @@ -69,6 +81,19 @@ def define_tables(cls, metadata):
Column("name", String(20)),
schema="schema",
)
# Add a foreign key example which crosses schema.
Table(
"number_colors",
metadata,
Column("ID", Integer, primary_key=True),
Column(
"number_id",
Integer,
ForeignKey("numbers.number", name="cross_schema_number_fk"),
),
Column("color", String(20)),
schema="schema",
)

def test_hello_world(self, connection):
greeting = connection.execute(text("select 'Hello World'"))
Expand All @@ -88,7 +113,7 @@ def test_reflect(self, connection):
engine = connection.engine
meta: MetaData = MetaData()
meta.reflect(bind=engine)
eq_(2, len(meta.tables))
eq_(3, len(meta.tables))
table = meta.tables["numbers"]
eq_(5, len(table.columns))
eq_("number", table.columns[0].name)
Expand Down Expand Up @@ -238,3 +263,40 @@ class User(Base):

eq_(len(inserted_rows), len(selected_rows))
eq_(set(inserted_rows), set(selected_rows))

def test_cross_schema_fk_lookups(self, connection):
"""Ensures we introspect FKs within & across schema."""

engine = connection.engine

insp = inspect(engine)
eq_(
{
(None, "number_colors"): [
{
"name": "number_fk",
"referred_table": "numbers",
"referred_schema": None,
"referred_columns": ["number"],
"constrained_columns": ["number_id"],
}
]
},
insp.get_multi_foreign_keys(filter_names=["number_colors"]),
)
eq_(
{
("schema", "number_colors"): [
{
"name": "cross_schema_number_fk",
"referred_table": "numbers",
"referred_schema": None,
"referred_columns": ["number"],
"constrained_columns": ["number_id"],
}
]
},
insp.get_multi_foreign_keys(
filter_names=["number_colors"], schema="schema"
),
)