Skip to content

[BUG] models depending on each other with foreign key constraints are executed simultaneously #1002

@ulsmo

Description

@ulsmo

Describe the bug
When creating a repro for #1001, both models were trying to execute simultaneously.
The model with the foreign key fails, as it tries to create a foreign key relation to a table that doesn't exist yet:

Table 'XXX.XXX.PRIMARY' does not exist or not authorized. (SQLSTATE: 42S02, Vendor code: 2003)

As the table with the primary key was created before, on the next dbt run the execution succeeded.
In dbt-core, the model with the foreign_key is executed after the successful creation of the model it references.
This builds on #653, where the ref('primary') was implemented to compile to valid sql.

What version of dbt Fusion is this bug in? (find out by running dbt --version) dbt-fusion 2.0.0-preview.59

Is this a discrepancy between the dbt Fusion Engine and dbt Core? Check one.

  • YES
  • NO

To Reproduce
Two models named primary.sql and foreign.sql with the definitions:

SELECT 1::int AS key

a schema.yml with the following config:

models:
  - name: primary
    config:
      materialized: table
      contract:
        enforced: true
    columns:
      - name: key
        data_type: int
        constraints:
          - type: primary_key

  - name: foreign
    config:
      materialized: table
      contract:
        enforced: true
    columns:
      - name: key
        data_type: int
        constraints:
          - type: foreign_key
            to: ref('primary')
            to_columns: [key]

run dbt build in a blank database - drop the tables between runs to reproduce the error consistently.

Expected behavior
primary model created first, after which the foreign model is created successfully.

Screenshots
Image

Operating System and CPU Type (please complete the following information):

  • Windows
  • X86
  • Snowflake

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions