dbt_constraints icon indicating copy to clipboard operation
dbt_constraints copied to clipboard

always_create_constraint is not working properly

Open RAJUUBHAI opened this issue 1 year ago • 1 comments

always_create_constraint config is working only with thresholds (warn_if and error_if) .. It is not forcing the constraint with just severity: warn.

-- Not setting the FK

  • name: col_fk tests: - dbt_constraints.foreign_key: pk_table_name: ref('pk_table') pk_column_name: col_pk config: severity: warn always_create_constraint: true

-- Setting the FK

  • name: col_fk tests: - dbt_constraints.foreign_key: pk_table_name: ref('pk_table') pk_column_name: col_pk config: warn_if: ">100" always_create_constraint: true

RAJUUBHAI avatar Dec 26 '23 06:12 RAJUUBHAI

Unfortunately, it is not working for us either. Maybe we got the syntax wrong? Any ideas are welcome!

  - name: account_id
    tests:
      - relationships:
          to: ref('account')
          field: account_id
          config:
            always_create_constraint: true
  - name: address_id
    tests:
      - dbt_constraints.foreign_key:
          pk_table_name: ref('address')
          pk_column_name: address_id
          config:
            severity: warn
            warn_if: ">621692"
            always_create_constraint: true

cos-pet-learn avatar Feb 07 '24 15:02 cos-pet-learn

@cos-pet-learn any luck regarding this issue ? how can we force dbt to create FK constraint in the table if even the FK test fails?

choudrybabu avatar Jun 05 '24 19:06 choudrybabu

I have completely overhauled the logic for always_create_constraint in version 1.0.0, just released. Now it isn't even necessary to use that parameter if you want to create a constraint for a test you executed but failed. dbt Constraints 1.0.0 will now always create NORELY constraints or alter a constraint from RELY to NORELY if a test is executed and fails.

You now only need always_create_constraint if you want to generate the constraint when you haven't even executed the test at all, for example with a dbt run.

sfc-gh-dflippo avatar Jul 25 '24 22:07 sfc-gh-dflippo