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

Updated constraints doc to include the new recommended syntax #6105

Closed
wants to merge 48 commits into from
Closed
Changes from 41 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
eca97e6
Updated constraints doc to include the new recommended syntax
nataliefiann Sep 20, 2024
bee1850
Merge branch 'current' into nfiann-constraints-foreignkey
mirnawong1 Sep 20, 2024
a7c4303
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
4ceebf5
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
3b29db8
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
147536f
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
51b7906
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
c614e0e
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
4d39385
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
29b9f99
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
878d3c6
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
eb80fc5
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
76ce5ee
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
5fcc692
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
ce6b8c2
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
62f61da
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
a952ac3
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
e895010
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
f7737bb
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
1bec002
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
5519154
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
4786527
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
214d990
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
3f8c49c
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
406f5ae
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
0168346
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
5586331
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
15e4b60
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
0464cce
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
0a1d6d4
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
6f1b976
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
34e5c4f
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
6f040dd
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
ae4887d
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
66b456d
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
8c96d99
Merge branch 'current' into nfiann-constraints-foreignkey
nataliefiann Sep 20, 2024
83c4489
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
1e4cf71
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
583fd26
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 20, 2024
2d62385
Updated constraints doc to show relevant syntax for v1.9
nataliefiann Sep 23, 2024
03e57e4
Merge branch 'current' into nfiann-constraints-foreignkey
mirnawong1 Sep 24, 2024
78352d5
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 24, 2024
7c443de
Merge branch 'current' into nfiann-constraints-foreignkey
mirnawong1 Sep 24, 2024
18eba31
updated doc to perfect version block
nataliefiann Sep 24, 2024
fb3da8e
Merge branch 'current' into nfiann-constraints-foreignkey
mirnawong1 Sep 25, 2024
0b87eff
Update constraints.md
mirnawong1 Sep 25, 2024
a7b7291
Update website/docs/reference/resource-properties/constraints.md
nataliefiann Sep 25, 2024
c3be91a
Merge branch 'current' into nfiann-constraints-foreignkey
mirnawong1 Sep 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 70 additions & 4 deletions website/docs/reference/resource-properties/constraints.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Constraints require the declaration and enforcement of a model [contract](/refer

**Constraints are never applied on `ephemeral` models or those materialized as `view`**. Only `table` and `incremental` models support applying and enforcing constraints.

<VersionBlock lastVersion="1.8">

## Defining constraints
nataliefiann marked this conversation as resolved.
Show resolved Hide resolved

Constraints may be defined for a single column, or at the model level for one or more columns. As a general rule, we recommend defining single-column constraints directly on those columns.
Expand All @@ -23,14 +25,10 @@ The structure of a constraint is:
- `name` (optional): Human-friendly name for this constraint. Supported by some data platforms.
- `columns` (model-level only): List of column names to apply the constraint over

<VersionBlock lastVersion="1.8">

When using `foreign_key`, you need to specify the referenced table's schema manually. Use `{{ target.schema }}` in the `expression` field to automatically pass the schema used by the target environment. Note that later versions of dbt will have more efficient ways of handling this.

For example: `expression: "{{ target.schema }}.customers(customer_id)"`

</VersionBlock>

<File name='models/schema.yml'>

```yml
Expand Down Expand Up @@ -68,9 +66,77 @@ models:
- type: ...
```

In [dbt Cloud Versionless](/docs/dbt-versions/upgrade-dbt-version-in-cloud#versionless), you can use the improved syntax to define foreign keys, which uses `ref`. This feature will be available in the upcoming 1.9 release. For more information, update the documentation version to 1.9 and refer to the [following section](/reference/resource-properties/constraints#whats-better-about-this-syntax).

</File>

</VersionBlock>

<VersionBlock firstVersion="1.9">
nataliefiann marked this conversation as resolved.
Show resolved Hide resolved

## Defining constraints

Constraints may be defined for a single column, or at the model level for one or more columns. As a general rule, we recommend defining single-column constraints directly on those columns.

If you are defining multiple `primary_key` constraints for a single model, those _must_ be defined at the model level. Defining multiple `primary_key` constraints at the column level is not supported.

The structure of a constraint is:
- `type` (required): one of `not_null`, `unique`, `primary_key`, `foreign_key`, `check`, `custom`
- `expression`: Free text input to qualify the constraint. Required for certain constraint types, and optional for others.
- `name` (optional): Human-friendly name for this constraint. Supported by some data platforms.
- `columns` (model-level only): List of column names to apply the constraint over

In [dbt Cloud Versionless](/docs/dbt-versions/upgrade-dbt-version-in-cloud#versionless), you can use the improved syntax to define foreign keys, which uses `ref`. This feature will be available in the upcoming 1.9 release.

The recommended syntax means dbt automatically resolves references to other models, ensuring they:

- Capture dependencies
- Work across different environments

Here's an example showing how the new syntax is used:
nataliefiann marked this conversation as resolved.
Show resolved Hide resolved

<File name='models/schema.yml'>
nataliefiann marked this conversation as resolved.
Show resolved Hide resolved

```yml

models:
nataliefiann marked this conversation as resolved.
Show resolved Hide resolved
nataliefiann marked this conversation as resolved.
Show resolved Hide resolved
name: <my_model>

# required
config:
contract:
enforced: true

nataliefiann marked this conversation as resolved.
Show resolved Hide resolved
# Model-level constraints
constraints:
nataliefiann marked this conversation as resolved.
Show resolved Hide resolved
- type: foreign_key
nataliefiann marked this conversation as resolved.
Show resolved Hide resolved
columns: [id]
nataliefiann marked this conversation as resolved.
Show resolved Hide resolved
to: ref('my_model_to') # or source('source', 'source_table')
to_columns: [id]
nataliefiann marked this conversation as resolved.
Show resolved Hide resolved
columns:
- name: id
nataliefiann marked this conversation as resolved.
Show resolved Hide resolved
data_type: integer
nataliefiann marked this conversation as resolved.
Show resolved Hide resolved

nataliefiann marked this conversation as resolved.
Show resolved Hide resolved

# Column-level definitions and constraints
columns:
nataliefiann marked this conversation as resolved.
Show resolved Hide resolved
- name: id
nataliefiann marked this conversation as resolved.
Show resolved Hide resolved
data_type: integer
nataliefiann marked this conversation as resolved.
Show resolved Hide resolved
constraints:
nataliefiann marked this conversation as resolved.
Show resolved Hide resolved
- type: foreign_key
to: ref('my_model_to') # or source('source', 'source_table')
to_columns: [id]


```

</File>


In dbt Cloud Versionless, you can now use the recommended syntax for defining foreign keys. This will become available in dbt Core v1.9.


</VersionBlock>

## Platform-specific support

Expand Down
Loading