databricks / dbt-databricks

A dbt adapter for Databricks.
https://databricks.com
Apache License 2.0
227 stars 119 forks source link

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

Closed PtrckGdwn closed 2 months ago

PtrckGdwn commented 2 months ago

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.

benc-db commented 2 months ago

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 commented 2 months ago

Ah, found the issue. Thanks for reporting.

benc-db commented 2 months ago

@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 commented 2 months ago

@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: https://github.com/dbt-labs/dbt-core/issues/8062#issuecomment-2316231542).

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.