diff --git a/core/dbt/context/providers.py b/core/dbt/context/providers.py index 470bfa67819..19465e9c7fd 100644 --- a/core/dbt/context/providers.py +++ b/core/dbt/context/providers.py @@ -481,6 +481,7 @@ def resolve( target_version: Optional[NodeVersion] = None, ) -> RelationProxy: target_model = self.manifest.resolve_ref( + self.model, target_name, target_package, target_version, diff --git a/core/dbt/exceptions.py b/core/dbt/exceptions.py index 625a964a001..7915fea4a32 100644 --- a/core/dbt/exceptions.py +++ b/core/dbt/exceptions.py @@ -1971,7 +1971,7 @@ def __init__(self, duped_name, unique_ids, node=None): def get_message(self) -> str: msg = ( f'dbt found multiple potential nodes when referencing "{self.duped_name}" - {self.unique_ids}.' - f"\nTo fix this, specify which package to use in 'ref' (one of: {self.packages})." + f"\nTo fix this, specify which package to use as the first argument to 'ref' (one of: {self.packages})." ) return msg diff --git a/test/unit/test_manifest.py b/test/unit/test_manifest.py index 187070f6283..0bcc8fd8a7b 100644 --- a/test/unit/test_manifest.py +++ b/test/unit/test_manifest.py @@ -37,6 +37,7 @@ ) from dbt.events.functions import reset_metadata_vars +from dbt.exceptions import AmbiguousResourceNameRefError from dbt.flags import set_from_args from dbt.node_types import NodeType @@ -1443,6 +1444,36 @@ def _refable_parameter_sets(): version=None, expected=None, ), + # duplicate node name across package + FindNodeSpec( + nodes=[MockNode("project_a", "my_model"), MockNode("project_b", "my_model")], + sources=[], + package="project_a", + version=None, + expected=("project_a", "my_model"), + ), + # duplicate node name across package: root node preferred to package node + FindNodeSpec( + nodes=[MockNode("root", "my_model"), MockNode("project_a", "my_model")], + sources=[], + package=None, + version=None, + expected=("root", "my_model"), + ), + FindNodeSpec( + nodes=[MockNode("root", "my_model"), MockNode("project_a", "my_model")], + sources=[], + package="root", + version=None, + expected=("root", "my_model"), + ), + FindNodeSpec( + nodes=[MockNode("root", "my_model"), MockNode("project_a", "my_model")], + sources=[], + package="project_a", + version=None, + expected=("project_a", "my_model"), + ), ] ) return sets @@ -1486,6 +1517,21 @@ def test_resolve_ref(nodes, sources, package, version, expected): assert result.package_name == expected_package +def test_resolve_ref_ambiguous_resource_name_across_packages(): + manifest = make_manifest( + nodes=[MockNode("project_a", "my_model"), MockNode("project_b", "my_model")] + ) + with pytest.raises(AmbiguousResourceNameRefError): + manifest.resolve_ref( + source_node=None, + target_model_name="my_model", + target_model_package=None, + target_model_version=None, + current_project="root", + node_package="root", + ) + + def _source_parameter_sets(): sets = [ # empties diff --git a/tests/functional/duplicates/test_duplicate_model.py b/tests/functional/duplicates/test_duplicate_model.py index 7a53fd6de63..17be1ff20b9 100644 --- a/tests/functional/duplicates/test_duplicate_model.py +++ b/tests/functional/duplicates/test_duplicate_model.py @@ -1,6 +1,6 @@ import pytest -from dbt.exceptions import CompilationError, DuplicateResourceNameError +from dbt.exceptions import CompilationError, AmbiguousAliasError from dbt.tests.fixtures.project import write_project_files from dbt.tests.util import run_dbt, get_manifest @@ -88,7 +88,7 @@ def test_duplicate_model_disabled_partial_parsing(self, project): assert len(results) == 1 -class TestDuplicateModelEnabledAcrossPackages: +class TestDuplicateModelAliasEnabledAcrossPackages: @pytest.fixture(scope="class") def models(self): return {"table_model.sql": enabled_model_sql} @@ -105,10 +105,10 @@ def setUp(self, project_root): def packages(self): return {"packages": [{"local": "local_dependency"}]} - def test_duplicate_model_enabled_across_packages(self, project): + def test_duplicate_model_alias_enabled_across_packages(self, project): run_dbt(["deps"]) - message = "dbt found two models with the name" - with pytest.raises(DuplicateResourceNameError) as exc: + message = "dbt found two resources with the database representation" + with pytest.raises(AmbiguousAliasError) as exc: run_dbt(["run"]) assert message in str(exc.value)