-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
[Bug] Unexpected ref behaviour when model name also exists in an imported package #11351
Comments
Thanks for providing the full details of a simple reproducible example @TomSteenbergen ! Did you try a workaround of the following?
DetailsHere's how to do them with your reprex: mv project_downstream/models/model_x/model_x.sql project_downstream/models/model_x/model_xy.sql
mv project_downstream/models/model_x/model_x.yml project_downstream/models/model_x/model_xy.yml Make the following changes within models:
- name: model_xy
config:
schema: downstream
alias: model_x Result
dbt docs generate
dbt docs serve ![]() ![]() dbt build ![]() |
Hi @dbeatty10 ! Thanks for the prompt reply 🙏 Ah yes, alias could be a workaround indeed! Besides needing to make some changes in our downstream queries, a downside I see is that it might be confusing for our engineers that prototyped a SQL query using this table and then want to translate it to a dbt model. In our dbt project the model name is always equal to the table name, so having to refer to it differently does introduce an opportunity of bugs to slip into our dbt models, which is not ideal. Perhaps this is something we will have to accept, but I do wonder if there is perhaps a way to solve this issue in |
Can the behavior be changed in
|
Agree with the ambiguity here. I have a strong preference for number 2. Two major reasons:
Regardless of the option we choose, I think it'd be great if we can also make the docs a bit less ambiguous on this case once we verify the current implementation indeed behaves as number 1. Thanks a lot @dbeatty10 for running this by your colleagues and taking the time to respond with some workarounds. 🙇 Let me know once you have an update! |
Thank you for providing those two reasons for your strong preference! Great insights 💡
💯 Opened this docs issue as a result: dbt-labs/docs.getdbt.com#6982 |
Is this a new bug in dbt-core?
Current Behavior
In our organization we have multiple dbt projects. The dbt projects of our central data warehouse team is oftentimes imported as a package in downstream dbt projects. These downstream dbt projects build their dbt models on top of their dbt models.
When the downstream dbt project contains a dbt model with a name that is also used by the upstream dbt project for another model, dbt can incorrectly ref to the wrong model.
For example:
project_upstream
andproject_downstream
project_upstream
is imported byproject_downstream
project_upstream
has two models:model_x
andmodel_y
model_y
referencesmodel_x
project_downstream
has one model, also calledmodel_x
model_x
references the upstreammodel_y
When running dbt in
project_downstream
, e.g.dbt compile
, dbt incorrectly thinks thatproject_upstream.model_y
is dependent onproject_downstream.model_x
instead ofproject_upstream.model_x
, resulting in a cyclic dependency sinceproject_downstream.model_x
referencesproject_upstream.model_y
.Expected Behavior
I would have expected that dbt, when using ref with a single argument in
project_upstream.model_y
, references the dbt model of the same project, i.e.project_upstream.model_x
, regardless of whether we are running dbt fromproject_upstream
orproject_downstream
. Instead, it seems to default to the project from which you are running dbt, even thoughproject_downstream
is not listed inpackages.yml
as a dependency ofproject_upstream
.Steps To Reproduce
I created a minimal example that you can easily run locally here: https://github.com/TomSteenbergen/dbt-ref-issue
Please clone that repo and refer to that repo's README for the steps to reproduce the issue.
Relevant log output
RuntimeError: Found a cycle: model.project_downstream.model_x --> model.project_upstream.model_y
Environment
Which database adapter are you using with dbt?
I encountered this error both using Snowflake and Postgres.
Additional Context
In the repo with the minimal example (https://github.com/TomSteenbergen/dbt-ref-issue) I also mention some workarounds I tried. One involves overriding the
ref
macro. While this should solve it, this yields another unexpected error:Please see the README of the above linked repo for the exact steps to reproduce.
The text was updated successfully, but these errors were encountered: