-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[CT-2782] [Bug] Error - cannot reference permanent table from temporary table constraint
when running incremental model with a foreign key
#8022
Comments
cannot reference permanent table from temporary table constraint
when running incremental model with a foreign keycannot reference permanent table from temporary table constraint
when running incremental model with a foreign key
I have the same issue on Postgres 15.x, dbt: 1.52, dbt-postgres:1.5.2 with an incremental model: i.e.
dim_partner:
fact_session:
|
@amardatar @LeviAndrewDixon Thanks for opening the issue, and for the reproduction case! I've confirmed that creating a temporary table with a FK constraint referencing a permanent table raises an error on Postgres & Redshift, but it does not raise an error on Snowflake. (BigQuery is an odder case: We only use "real" temp tables on BigQuery, which need to be run inside of a multi-statement "script," for the It's true that foreign key constraints are not very well supported in dbt right now, especially since we don't support Still, it's undesirable behavior for FK constraints to work only sometimes for incremental models on Postgres/Redshift. Options:
I'm supportive of pursuing one of those, and option (1) feels cleaner. The next steps would be:
|
Thanks for the update @jtcohen6!
I think it's worth mentioning just regarding this point - for Redshift specifically, foreign keys do carry special meaning (despite not being enforced) - as per point 2 in this docs page a foreign key will be used for collocation of data, which is relevant for performance. I know - this is niche, and Redshift itself doesn't have the popularity of other warehouses, so I don't expect this to be high priority - but also figured it was relevant knowledge to share since it is more than just purely informational for Redshift. In terms of the options you mentioned - don't know if you're particularly looking for our input here or if that's just a decision that needs to be made internally; for my part I'd agree that (1) seems the better option (otherwise this potentially goes down the track of different adapters having different constraints rendered at different times, and confusion on what exactly to expect). |
@amardatar Fair point! Snowflake supports something similar—using the constraint as an input to query rewrite/optimization—by specifying |
Note from refinement meeting:
|
Not enforced but, as mentioned above, are used in various ways for optimization - note BigQuery now also supports the definition of unenforced primary and foreign keys for query optmization purposes: https://cloud.google.com/blog/products/data-analytics/join-optimizations-with-bigquery-primary-and-foreign-keys?hl=en |
I verified a fix for Postgres in #8768, created a backlog ticket for Redshift here: dbt-labs/dbt-redshift#628. |
Is this a new bug in dbt-core?
Current Behavior
When running an incremental model with contract enforced and a foreign key, the model will fail on subsequent updates.
Expected Behavior
The update should succeed without an error.
Steps To Reproduce
First run a statement like:
Then, given a
schema.yml
file:And a SQL file:
dbt will succeed on the first run, and subsequently fail with the error:
As per the error returned, the issue appears to be that while rendering a temporary table as part of the update, dbt will also render the foreign key constraint (which is not valid in the context of a temporary table). This leads to the above failure.
Relevant log output
No response
Environment
Which database adapter are you using with dbt?
redshift
Additional Context
While I've run into this issue using Redshift (and using the
dbt-redshift
adapter) I've put this bug indbt-core
as I believe from a bit of research that the same issue would occur if this were run in PostgreSQL (and possibly in other data warehouses, although it was harder to research the issue broadly).The text was updated successfully, but these errors were encountered: