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

[CT-3544] [Bug] Model level constraints aren't being applied to versioned models #9364

Closed
2 tasks done
jeremyyeo opened this issue Jan 11, 2024 · 0 comments · Fixed by #9539
Closed
2 tasks done

[CT-3544] [Bug] Model level constraints aren't being applied to versioned models #9364

jeremyyeo opened this issue Jan 11, 2024 · 0 comments · Fixed by #9539
Assignees
Labels
backport 1.7.latest bug Something isn't working High Severity bug with significant impact that should be resolved in a reasonable timeframe

Comments

@jeremyyeo
Copy link
Contributor

jeremyyeo commented Jan 11, 2024

Is this a new bug in dbt-core?

  • I believe this is a new bug in dbt-core
  • I have searched the existing issues, and I could not find an existing issue for this bug

Current Behavior

If you're using model versions (https://docs.getdbt.com/docs/collaborate/govern/model-versions), model level constraints (https://docs.getdbt.com/reference/resource-properties/constraints#defining-constraints) don't seem to be applied to the model.

Expected Behavior

Model level constraints should be applied as expected.

Steps To Reproduce

First let's look at a well behaved model level constraints example (without model versioning).

# dbt_project.yml
name: my_dbt_project
profile: all
config-version: 2
version: "1.0.0"

models:
  my_dbt_project:
    +materialized: table

# models/schema.yml
version: 2
models:
  - name: foo
    config:
      contract:
        enforced: true
    constraints:
      - type: primary_key
        columns: [id, user_name]
    columns:
      - name: id
        data_type: int
        constraints:
          - type: not_null
      - name: user_name
        data_type: text
-- models/foo.sql
select 1 as id, 'alice' as user_name

Do a run:

$ dbt --debug run -s foo
...
23:06:58  On model.my_dbt_project.foo: /* {"app": "dbt", "dbt_version": "1.7.4", "profile_name": "all", "target_name": "pg-local", "node_id": "model.my_dbt_project.foo"} */

  
    

  create  table "postgres"."public"."foo__dbt_tmp"
  
  
    
  
  (
    id int not null,
    user_name text,
    
    primary key (id, user_name)
    )
 ;
    insert into "postgres"."public"."foo__dbt_tmp" (
      id, user_name
    )
  
  (
    
    select id, user_name
    from (
        select 1 as id, 'alice' as user_name
    ) as model_subq
  );
  
23:06:58  SQL status: INSERT 0 1 in 0.0 seconds
...

We see the constraint primary key (id, user_name) is applied.

Now let's rename the file foo.sql > foo_v1.sql and edit the schema.yml files contents:

# models/schema.yml
version: 2
models:
  - name: foo
    latest_version: 1
    config:
      contract:
        enforced: true
    constraints:
      - type: primary_key
        columns: [id, user_name]
    columns:
      - name: id
        data_type: int
        constraints:
          - type: not_null
      - name: user_name
        data_type: text
    versions:
      - v: 1
-- models/foo_v1.sql
select 1 as id, 'alice' as user_name

Do a run:

$ dbt clean && dbt --debug run -s foo
...
23:09:56  On model.my_dbt_project.foo.v1: /* {"app": "dbt", "dbt_version": "1.7.4", "profile_name": "all", "target_name": "pg-local", "node_id": "model.my_dbt_project.foo.v1"} */

  
    

  create  table "postgres"."public"."foo_v1__dbt_tmp"
  
  
    
  
  (
    id int not null,
    user_name text
    
    )
 ;
    insert into "postgres"."public"."foo_v1__dbt_tmp" (
      id, user_name
    )
  
  (
    
    select id, user_name
    from (
        select 1 as id, 'alice' as user_name
    ) as model_subq
  );
  
23:09:56  SQL status: INSERT 0 1 in 0.0 seconds
...

A versioned model of foo is created (foo_v1) and column level contraints are applied but the model level constraint is not applied.

Relevant log output

No response

Environment

- OS: macOS
- Python: 3.11.2
- dbt: 
Core:
  - installed: 1.7.4
  - latest:    1.7.4 - Up to date!

Plugins:
  - bigquery:  1.7.2 - Up to date!
  - snowflake: 1.7.1 - Up to date!
  - postgres:  1.7.4 - Up to date!

Which database adapter are you using with dbt?

postgres

Additional Context

No response

@jeremyyeo jeremyyeo added bug Something isn't working triage labels Jan 11, 2024
@github-actions github-actions bot changed the title [Bug] Model level constraints aren't being applied to versioned models [CT-3544] [Bug] Model level constraints aren't being applied to versioned models Jan 11, 2024
@dbeatty10 dbeatty10 removed the triage label Jan 11, 2024
@graciegoheen graciegoheen added High Severity bug with significant impact that should be resolved in a reasonable timeframe backport 1.7.latest labels Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 1.7.latest bug Something isn't working High Severity bug with significant impact that should be resolved in a reasonable timeframe
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants