diff --git a/dbtmetabase/manifest.py b/dbtmetabase/manifest.py index 2d88f40..93bda19 100644 --- a/dbtmetabase/manifest.py +++ b/dbtmetabase/manifest.py @@ -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. @@ -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, }