Skip to content

Primary key constraint applied without contract #1342

@dbeatty10

Description

@dbeatty10

Describe the bug

Constraints should only be applied for enforced contracts, but after #1296 they are also being added when a contract is not enforced.

When there are NULL values in the column specified as a primary key constraint, this will cause builds to fail, which is a regression from previous behavior.

Steps To Reproduce

As shared by @chadw56 in dbt-labs/dbt-core#12400 (comment):

- name: my_second_dbt_model
  description: "A starter dbt model"
  config:
    materialized: incremental
  constraints:
    - type: primary_key
      columns: [id]
  columns:
      - name: id
        description: "The primary key for this table"
        tests:
            - unique
            - not_null

Run the model and see that a constraint was added.

In some cases this can cause database errors like the following:

Database Error in model c_financial_accounting_gl_detail 
  Cannot create the primary key `c_financial_accounting_gl_detail_pk` because its child column(s) `seqnbr` is nullable.

Expected behavior

Constraints should only be applied for enforced contracts. The constraint should not be applied in this case because the model contract is not enforced.

Proposed fix

Proposed fix here:
dbt-labs/dbt-core#12400 (comment)

System information

The output of dbt --version:

- dbt-databricks (working): 1.11.3post1+cd5d71f50cd9f003e2f879dc6729a3aa93e1cdaf
- dbt-databricks (regression): 1.11.4post1+ec229f85e80de8737ba308b58059fef56cf9e3fd

Additional context

Originally reported in dbt-labs/dbt-core#12400. Root cause explained in dbt-labs/dbt-core#12400 (comment).

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