Get started with Secoda
See why hundreds of industry leaders trust Secoda to unlock their data's full potential.
See why hundreds of industry leaders trust Secoda to unlock their data's full potential.
dbt model contracts are a set of model config properties that enforce specific data types, columns, and constraints in a model. They are primarily used to define the shape of a model's API response and ensure that any changes in data do not disrupt the functionality of downstream consumers' data products.
models:
- name: my_model
columns:
- name: column1
tests:
- not_null
- unique
In the above code, a dbt model contract is defined for a model named 'my_model'. It specifies that 'column1' should not contain any null values and should be unique.
dbt supports three types of contracts: SQL models, Materializations, and Incremental models. Each type serves a unique purpose and is used in different scenarios depending on the requirements of the data team.
models:
- name: my_model
materialized: incremental
columns:
- name: column1
tests:
- not_null
- unique
The code snippet above illustrates an incremental model contract. The 'materialized: incremental' line indicates that the model will only update with new data, rather than rebuilding the entire model each time.
When a model is built with a defined contract, dbt will run a "preflight" check to ensure the model's query returns the correct column names and data types. It will then include these details in the DDL statements sent to the data platform and enforce the constraints while building or updating the model's table.
models:
- name: my_model
columns:
- name: column1
tests:
- not_null
- unique
The code above shows a dbt model contract. When this model is built, dbt will ensure that 'column1' does not contain any null values and is unique. If these conditions are not met, the build will fail.
If the column list or data types in a model do not match the contract, the build will fail and highlight the discrepancy. This ensures that the integrity of the data is maintained and any issues are identified early in the process.
models:
- name: my_model
columns:
- name: column1
tests:
- not_null
- unique
In the code above, if 'column1' contains null values or is not unique, the build will fail, and dbt will highlight these issues.
A model contract includes the column name, column data type, a rule for no null values, and any additional constraints or validation checks. These contracts are defined in yaml (structured data) and are available in dbt metadata. They can be defined by the same or different people from those writing the model's SQL.
models:
- name: my_model
columns:
- name: column1
data_type: integer
tests:
- not_null
- unique
The code snippet above defines a model contract for 'my_model'. It specifies that 'column1' should be of integer data type, should not contain any null values, and should be unique.