9 min readIntermediate§ dbt

dbt Model Contracts and Versions for Shared Models

Use dbt model contracts, versions, access levels and deprecation dates to change a widely used model without breaking the dashboards and teams that use it.

By Dinesh ChandraSenior Data and AI Engineer · SnowPro certified
Illustrated overview of dbt Model Contracts and Versions for Shared Models
Fig. 1 · dbt
  1. 1.An enforced contract makes dbt check column names and data types before it builds the model, and fail the build if the query does not match.
  2. 2.Contracts turn a removed column or a changed type into a CI error when dbt compares against production state.
  3. 3.Versions let you ship a breaking change as v2 while consumers stay on v1 until a published deprecation date.
  4. 4.On Snowflake only not_null constraints are enforced; primary_key, unique and foreign_key are metadata.
  5. 5.Access levels (private, protected, public) and groups decide who may ref a model in the first place.
Table of contents

Every analytics team has a model like dim_customers. Forty downstream models read it, three dashboards query it directly, and another team’s project depends on it. Someone renames a column, the pull request passes, and on Monday morning half the company’s reports are blank.

dbt has had the tools to prevent this since v1.5: model contracts, model versions, and access controls. They are often skipped because they look like paperwork. Used on the right few models, they turn “we broke finance’s dashboard” into a failed CI check with a clear message, and give you a documented way to make the change anyway.

This post shows how the three pieces fit together on a real model, what Snowflake does and does not enforce, and which models deserve this treatment.

The problem

A dbt model’s output shape is an implicit promise. Its columns, names and types are what consumers code against. Nothing in a default dbt project writes that promise down, so nothing checks it.

Data tests do not close the gap. A not_null test on customer_id tells you about the rows, not about whether customer_name still exists or whether lifetime_value silently changed from a number to a string. Tests run after the table has already been replaced. By then, the consumer has already seen the change.

The common wrong approach

The usual workaround is social: a Slack message announcing the rename, a comment in the YAML saying “do not change without asking finance”, and a hope that people read both. The other workaround is technical but blunt: never change shared models at all, and pile new columns onto them forever. Both approaches fail the moment the team changes or the model needs a real redesign.

The approach that works

Use the three features in order. A contract defines the shape. A version lets you change the shape without breaking consumers. Access levels decide who is allowed to be a consumer.

flowchart LR
  c["Contract: enforced shape"] --> v["Versions: v1 and v2 side by side"]
  v --> d["deprecation_date on v1"]
  d --> a["Access: public, protected, private"]

Contract the shape, version the change, control who can depend on it.

Step 1: Enforce a contract

An enforced contract must list every column with its name and a data_type your warehouse understands:

# models/marts/_dim_customers.yml
models:
  - name: dim_customers
    config:
      materialized: table
      contract:
        enforced: true
    columns:
      - name: customer_id
        data_type: int
        constraints:
          - type: not_null
          - type: primary_key
      - name: customer_name
        data_type: varchar
      - name: country_name
        data_type: varchar
      - name: lifetime_value
        data_type: number(38, 2)

With the contract enforced, dbt does two extra things on every build. It runs a preflight check that the query returns exactly these column names and types, ignoring order. Then it puts the names, types and constraints into the DDL and orders the columns as the contract lists them. If the SQL drifts from the YAML, the build fails before the table is replaced.

Contracts work on table and view models, and on incremental models only when on_schema_change is append_new_columns or fail. Views get names and types, but not constraints. Python models, ephemeral models, materialized views and custom materializations are not supported. On Snowflake, the dynamic_table materialization does not support contracts either.

Step 2: Know what Snowflake actually enforces

Constraints differ by warehouse. On Snowflake, per the dbt docs:

  • not_null is defined and enforced.
  • primary_key, unique and foreign_key are defined but not enforced. They exist as metadata.
  • check constraints are not supported.

So the primary_key in the example documents intent for tools and people; it does not stop duplicates. Keep a unique data test on the key, as described in the testing strategy guide.

Step 3: Let CI catch breaking changes

Contracts pay off in CI. When dbt compares the project against a previous state, it looks for breaking changes and raises a contract error. The documented breaking changes are removing a column, changing a column’s data_type, removing or modifying a constraint on an existing column, and removing a contracted model by deleting, renaming or disabling it.

If you already run Slim CI with state and defer, you get this check for free:

dbt build --select state:modified+ --defer --state prod-artifacts

A pull request that drops country_name from dim_customers now fails with a contract error instead of reaching production.

Step 4: Ship the breaking change as a new version

Sometimes the breaking change is correct. Versions let you publish it next to the old shape. The recommended style declares shared properties once and lists only the differences per version:

# models/marts/_dim_customers.yml
models:
  - name: dim_customers
    latest_version: 1
    config:
      materialized: table
      contract:
        enforced: true
    columns:
      - name: customer_id
        data_type: int
      - name: customer_name
        data_type: varchar
      - name: country_name
        data_type: varchar
    versions:
      - v: 1
        deprecation_date: 2027-03-31
      - v: 2
        columns:
          - include: all
            exclude: [country_name]

By convention each version lives in its own file, dim_customers_v1.sql and dim_customers_v2.sql, and builds a relation named dim_customers_v1 and dim_customers_v2. Because latest_version is still 1, version 2 is a prerelease. Consumers can test it explicitly:

select * from {{ ref('dim_customers', v=2) }}

An unpinned ref('dim_customers') keeps resolving to the latest version, and dbt prints a notice that a prerelease is available. When you are ready, change latest_version to 2.

Step 5: Keep a stable name for direct queries

Dashboards that query analytics.dim_customers directly do not use ref. The latest_version_pointer config creates a view with the base model name that always points at the latest version. It is available from dbt v1.12 and is on by default in dbt v2. On v1.12 you can enable it project-wide:

# dbt_project.yml
flags:
  latest_version_pointer_enabled_by_default: true

Do not also give the latest version an alias equal to the base name; dbt raises error dbt1005 for that collision.

Step 6: Restrict who can depend on the model

Groups and access levels limit the blast radius in advance. A private model can only be referenced inside its group, a protected model inside the project, and a public model from anywhere, including other projects:

# models/marts/_groups.yml
groups:
  - name: customer_success
    owner:
      name: Customer Success Analytics
      email: cs-analytics@example.com

models:
  - name: dim_customers
    config:
      group: customer_success
      access: public
  - name: int_customer_history_rollup
    config:
      group: customer_success
      access: private

The default access is protected, mainly for backward compatibility; the docs recommend private for new models. A ref that breaks the rule fails at parse time with a DbtReferenceError.

Edge cases and changes

Deprecation warnings are real signals. Once deprecation_date is set, dbt warns producers and consumers with DeprecatedModel, DeprecatedReference and UpcomingReferenceDeprecation. You can promote these to errors with WARN_ERROR_OPTIONS in CI. A model with an enforced contract cannot be deleted before its deprecation date.

Type aliasing. dbt translates generic names like string to the platform type. If you want the exact type you wrote, set alias_types: false in the contract config. For numbers, specify precision and scale, such as number(38, 2), to avoid implicit coercion.

Cross-project refs. With dbt Mesh on the dbt platform’s Enterprise plans, another project can reference your public model through a project dependency:

select * from {{ ref('customer_platform', 'dim_customers', v=2) }}

Without Enterprise, installing the project as a package works, but access restrictions are off for package models unless the producer sets restrict-access: True.

dbt v2 strictness. In dbt v2, contracts work the same way, but the stricter parser also rejects misspelled keys in these YAML blocks. A typo in contract: that v1 ignored will now fail. The dbt v2 upgrade guide covers that.

Trade-offs

Contracts add YAML. Every column needs a name and a type, and the docs are explicit that partial contracts are not a thing. For a 200-column model, that is a real maintenance cost.

Versions add relations. Each live version is a separate table or view that you build, test and pay for until it is retired. Two versions are a migration; five are a backlog.

The answer is to be selective. Put contracts on the public interface of your project: the marts other teams, dashboards and projects read. Leave staging and intermediate models flexible, ideally private, so you can refactor them freely. The project structure guide shows where that boundary usually falls.

Pitfalls

  • Contract on an incremental without on_schema_change. New columns never reach the table. Set append_new_columns or fail.
  • Trusting primary_key on Snowflake. It is not enforced. Keep a unique data test.
  • Versions with no end date. Old versions live forever. Set deprecation_date when you create v2.
  • Pinning every consumer. Pinned refs never move. Leave internal refs unpinned and pin only during a migration.
  • Contracts everywhere. Staging models change often. Contract the marts others depend on.

FAQ

Is a dbt contract the same as a data test?

No. A contract checks the shape of the output, column names and data types, before the model is built. A data test checks the rows after it is built. You need both on important models.

What happens if my SQL returns an extra column?

The preflight check compares the query’s columns with the contract and fails the build on a mismatch. Add the column to the YAML, which is not a breaking change, or remove it from the SQL.

Do I need versions to use contracts?

No. Contracts work alone and still catch breaking changes in CI. Versions are for when you decide a breaking change is necessary and need consumers to migrate on a schedule.

How do I retire an old version?

Set deprecation_date on it, watch the deprecation warnings to find remaining consumers, and remove the version after the date passes. dbt prevents deleting a contracted model before its deprecation date.

Can I use contracts on dynamic tables in Snowflake?

Not today. The dbt Snowflake adapter docs list model contracts as unsupported for the dynamic_table materialization. Use a table or incremental model for the contracted interface if you need one.

What this means for your pipelines

You do not need contracts on every model. You need them on the small set of models that other people depend on. Contract those, mark them public, and mark everything behind them private. Your CI then fails on the change that would have broken a consumer, and the error names the column.

When a breaking change is the right call, ship it as a new version with a deprecation date on the old one. Consumers get a prerelease to test, a warning while they wait, and a stable name through the latest-version pointer. That is the difference between a shared model and a shared liability.

Sources: Model contracts, contract config, Model versions, latest_version_pointer, deprecation_date, Model access, Project dependencies, Snowflake configurations.

Share this post:X / TwitterLinkedInRedditWhatsApp
D

Dinesh Chandra

Senior Data & AI Engineer · SnowPro Core & Gen AI Certified

4+ years building production data platforms on Snowflake, Databricks, AWS, and Azure. I write from hands-on experience with Medallion architectures, dbt, Airflow, PySpark, and Snowflake Cortex AI. Every tutorial on this blog reflects real pipeline decisions, not documentation rewrites.

Read more about the author →
RE

Related entries

§ dbtIntermediate
Illustrated overview of dbt Microbatch: Incremental Models Built by Time Batch

· 8 min

dbt Microbatch: Incremental Models Built by Time Batch

How the dbt microbatch strategy works: event_time, batch_size, lookback, targeted backfills, retrying failed batches, and when it beats a hand-written filter.

  • #dbt
  • #incremental-models

Search DataLane

↑↓ navigatealt ↑↓ jump group↵ openesc close