Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing
Back
  1. Home
  2. Interview prep
  3. Model contracts

dbt · Modern dbt Features

Model contracts

Mediumdbt-40
contractsgovernanceschema-enforcementdbt-mesh

Question

What is a dbt model contract?

Solution

A dbt model contract is an explicit schema guarantee declared in YAML that enforces column names, data types, and constraints before building a model. When you configure contract enforced true, dbt verifies that the compiled query output matches the declared schema definition. If the model drops a declared column, adds an undeclared column, or returns the wrong data type, the build fails immediately.

Contract configuration

You specify contracts inside the model definition block in YAML. Every column in the model must be explicitly declared with its target data type:

models:
  - name: fct_orders
    config:
      contract:
        enforced: true
    columns:
      - name: order_id
        data_type: integer
        constraints:
          - type: not_null
          - type: primary_key
      - name: order_amount
        data_type: numeric
      - name: order_status
        data_type: string

The compiler uses this schema specification to enforce column validation.

Warehouse constraints and guarantees

Beyond compiler-level checks, dbt translates declared column constraints into physical DDL constraints on warehouses that support them, such as Snowflake, BigQuery, and Postgres.

Key features enforced by contracts include:

  • Data type matching confirms that the SQL expression returns the exact type required by downstream consumers, preventing implicit cast failures.
  • Physical constraints pass not-null, primary key, and unique rules to database engines to support query optimizer indexing and warehouse integrity checks.
  • Fail-fast builds block schema drift before corrupted tables get written to the warehouse.

This ensures downstream analysts receive dependable structures.

Protecting downstream teams

In larger organizations, data models act as application programming interfaces. If an upstream engineer refactors int_orders_joined and inadvertently renames order_id to id, downstream dashboards and mart tables break without warning.

Contracts establish a clear boundary between producer and consumer models. When paired with model access settings and model versioning, contracts enable cross-project dependencies in dbt Mesh. Downstream teams can safely query a public model knowing its contract prevents unexpected schema modifications.

PreviousNext