Skip to content

Commit

Permalink
Fix incorrect self-referencing foreign keys
Browse files Browse the repository at this point in the history
  • Loading branch information
gouline committed Jan 25, 2024
1 parent 5e62ca4 commit 3ea53a8
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions dbtmetabase/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,12 @@ def _read_relationships(
relationships = {}

for child_id in manifest["child_map"][unique_id]:
child = {}
if manifest[group]:
child = manifest[group].get(child_id, {})
child = manifest.get(group, {}).get(child_id, {})

if (
child.get("resource_type") == "test"
and child.get("test_metadata", {}).get("name") == "relationships"
and child.get("attached_node") == unique_id
):
# To get the name of the foreign table, we could use child[test_metadata][kwargs][to], which
# would return the ref() written in the test, but if the model has an alias, that's not enough.
Expand Down Expand Up @@ -204,22 +203,23 @@ def _read_relationships(

depends_on_id = depends_on_nodes[0]

fk_model = manifest[group].get(depends_on_id, {})
fk_target_table_alias = fk_model.get(
"alias", fk_model.get("identifier", fk_model.get("name"))
fk_target_model = manifest[group].get(depends_on_id, {})
fk_target_table = fk_target_model.get(
"alias",
fk_target_model.get("identifier", fk_target_model.get("name")),
)

if not fk_target_table_alias:
if not fk_target_table:
_logger.debug("Cannot resolve dependency for '%s'", depends_on_id)
continue

fk_target_schema = manifest[group][depends_on_id].get(
"schema", DEFAULT_SCHEMA
)
fk_target_table = f"{fk_target_schema}.{fk_target_table}"
fk_target_field = child["test_metadata"]["kwargs"]["field"].strip('"')

relationships[child["column_name"]] = {
"fk_target_table": f"{fk_target_schema}.{fk_target_table_alias}",
"fk_target_table": fk_target_table,
"fk_target_field": fk_target_field,
}

Expand Down

0 comments on commit 3ea53a8

Please sign in to comment.