dbt-labs / dbt-labs/dbt

[Feature] populate model['constraints'] even if the contract is not enforced

Open
#10,219 6 comments 4 reactions 0 assignees View on GitHub
engine:v1 model_contracts type:feature
Dominant language
Rust
Stars
13.8k
Forks
2.6k
Avg merge
21h 31m
Merged PRs (30d)
56

Description

### Is this your first time submitting a feature request?

- [X] I have read the [expectations for open source contributors](https://docs.getdbt.com/docs/contributing/oss-expectations)
- [X] I have searched the existing issues, and I could not find an existing issue for this feature
- [X] I am requesting a straightforward extension of existing dbt functionality, rather than a Big Idea better suited to a discussion

### Describe the feature

From @kmarq in https://github.com/dbt-labs/dbt-core/issues/10195:

Recreated some previous testing of constraints. If you define a primary_key constraint on a column

```
columns:
- name: col_1
data_type: string
constraints:
- type: not_null
- type: primary_key
```
The constraint shows up in the model.columns
```
"columns": {
"col_1": {
"constraints": [
{
"type": "not_null",
"warn_unenforced": true,
"warn_unsupported": true
},
```
whether the contract enforcement is true or false.

If you define a composite primary_key on the model
```
models:
- name: model_a
config:
contract:
enforced: true
description: description
columns:
- name: col_1
data_type: string
constraints:
- type: not_null
- type: primary_key
- name: col_2
data_type: string
constraints:
- type: not_null
constraints:
- type: primary_key
columns: [col_1, col_2]
```

Then in the model.constraints you will see this ONLY if contract enforcement is true:
```
"constraints": [
{
"columns": [
"col_1",
"col_2"
],
"type": "primary_key",
"warn_unenforced": true,
"warn_unsupported": true
}
],
"contract": {
"alias_types": true,
"checksum": "65ba38db6e4dd6a88418461c76b805acd2e39a113404a8a9c3d556c020564ea4",
"enforced": true
},
```
compared to false:
```
"constraints": [],
"contract": {
"alias_types": true,
"enforced": false
},
```

It [looks like](https://github.com/dbt-labs/dbt-core/blob/366d4ad04a8db6e3b52dc7497809b4415254bcbe/core/dbt/parser/schemas.py#L897-L911) we're parsing model-level constraints (populating model['constraints']) only if contract enforcement is enabled.

### Acceptance Criteria

- populate model['constraints'] even if the contract is not enforced
- fixing this might technically be a "breaking" change, insofar as it will start raising a parsing error (where it didn't previously) if you have an invalid constraint type defined on a model that doesn't have an enforced contract. So in that case we'd just want it to be a warning instead

### Who will this benefit?

_No response_

### Are you interested in contributing this feature?

_No response_

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.