Skip to content
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

Foreign Key constraint error: No parent table defined for foreign key #784

Closed
PtrckGdwn opened this issue Sep 10, 2024 · 4 comments · Fixed by #789
Closed

Foreign Key constraint error: No parent table defined for foreign key #784

PtrckGdwn opened this issue Sep 10, 2024 · 4 comments · Fixed by #789
Labels
bug Something isn't working

Comments

@PtrckGdwn
Copy link

Describe the bug

When trying to create a foreign key constraint on a relation using the parent and parent_columns constraint attributes, I am getting an error at run time: No parent table defined for foreign key which is followed by a trace.

Steps To Reproduce

I have configured the model's schema like so:

  - name: example__child_table
    constraints:
      - type: primary_key
        name: pk_example__child_table
        columns: ["id"]
      - type: not_null
        columns: ["id", "name", "parent_id"]
      - type: foreign_key
        name: fk_example__child_table_1
        columns: ["parent_id"]
        parent: parent_table
        parent_columns: ["id"]
    columns:
      - name: id
      - name: name
      - name: parent_id

When running the project this results in the error message described above.

Expected behavior

The foreign key sql should be generated, and assume all else is good (e.g., the parent_table has a primary key) the foreign key should be created.

Screenshots and log output

If applicable, add screenshots or log output to help explain your problem.

System information

The output of dbt --version:

Running with dbt=1.8.6
Registered adapter: databricks=1.8.5

The operating system you're using:
Running as a dbt task in a Databricks workflow.
Compute: Single node: Standard_DS3_v2 · DBR: 14.3.x-photon-scala2.12

Additional context

The traditional expression method of creating a foreign key works as documented, but I would like to avoid using it because it has a hard coded schema name. The parent + parent_columns method assumes that the parent table is in the same schema as the child, which for me is true.

@PtrckGdwn PtrckGdwn added the bug Something isn't working label Sep 10, 2024
@benc-db
Copy link
Collaborator

benc-db commented Sep 13, 2024

Will need to investigate why this isn't working...looking at the macro def, it looks as though it should. Do you mind sharing the dbt.log with me via email? ben.cassell@databricks.com

@benc-db
Copy link
Collaborator

benc-db commented Sep 13, 2024

Ah, found the issue. Thanks for reporting.

@benc-db
Copy link
Collaborator

benc-db commented Sep 13, 2024

@PtrckGdwn are there any earlier versions where this does work? I'm trying to narrow down whether a.) this never worked or b.) it used to work but dbt Labs changed how constraint config is validated.

@PtrckGdwn
Copy link
Author

@benc-db I don't know if there are any earlier versions that worked. I suspect the issue may be due to the introduction of some support for a similar method of adding fk constraints that was added to dbt core recently (though apparently will not be operational until v1.9 - see: dbt-labs/dbt-core#8062 (comment)).

One thing to note though about the dbt core change is that I believe it will add support for ref('my_model') against the the 'to' attribute. This would be a awesome thing to have flow through to dbt-databricks, because at present FKs are not part of the dag and you need to use explicit depends_on statements in models to ensure they build in the correct order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants