dbt graph and relation contracts
dbt code compiles a dependency graph into warehouse-specific SQL and executes
selected resources. Distinguish source text, compiled SQL, manifest/run results,
and actual warehouse relations.
Workflow
- Inspect dbt implementation/version, adapter, project/profile configuration,
targets, package locks, existing conventions, model contracts, state
artifacts, and warehouse authority. Never print credentials.
- Define each model's entity and grain, unique key, columns, null policy,
freshness, and materialization before writing SQL.
- Use
source() for declared raw sources and ref() for dbt dependencies.
Do not hard-code environment-specific database/schema names where graph
references provide the contract.
- Keep one semantic grain per CTE/model. Add generic or singular tests for
uniqueness, not-null, relationships, allowed values, reconciliation, and
business invariants proportionate to consequence.
- Choose view/table/incremental/ephemeral from consumer, cost, rebuild, and
lineage needs. An incremental model must define cutoff, late-arrival,
updates/deletes, unique key, schema change, and full-refresh behavior.
- Compile first and inspect target SQL. Run the narrow selector in a safe
target, then tests; use
dbt build when ordered model/seed/snapshot/test
execution is intended.
- Inspect
manifest.json, run_results.json, relation row/grain checks, and
warehouse query behavior before declaring completion.
Invariants
ref() establishes graph order but does not prove join cardinality or model
grain.
- Tests are executable contracts, not documentation decoration.
- Incremental correctness includes late and changed records; a fast append-only
query is wrong when source semantics permit updates.
- Macros generate SQL and can hide dialect behavior. Inspect compiled SQL and
test every supported adapter branch.
- Snapshots model change history; define check/timestamp strategy and invalidate
hard deletes deliberately.
with orders as (
select * from {{ ref('stg_orders') }}
),
customer_month as (
select customer_id, date_trunc('month', ordered_at) as month,
sum(net_revenue) as net_revenue
from orders
group by 1, 2
)
select * from customer_month
Read graph and model design, incremental and snapshot
semantics, and compile/run verification.
1---2name: dbt-sql3description: >-4 Use for creating, reviewing, debugging, or testing dbt SQL projects involving5 models, sources, ref, tests, macros, materializations, incremental models,6 snapshots, seeds, selectors, compiled SQL, or dbt artifacts. Do not use for7 generic SQL with no dbt graph, warehouse administration, semantic-layer8 product configuration, or orchestration outside dbt.9argument-hint: "[dbt model or project task, adapter, grain, materialization, and tests]"10---1112# dbt graph and relation contracts1314dbt code compiles a dependency graph into warehouse-specific SQL and executes15selected resources. Distinguish source text, compiled SQL, manifest/run results,16and actual warehouse relations.1718## Workflow19201. Inspect dbt implementation/version, adapter, project/profile configuration,21 targets, package locks, existing conventions, model contracts, state22 artifacts, and warehouse authority. Never print credentials.232. Define each model's entity and grain, unique key, columns, null policy,24 freshness, and materialization before writing SQL.253. Use `source()` for declared raw sources and `ref()` for dbt dependencies.26 Do not hard-code environment-specific database/schema names where graph27 references provide the contract.284. Keep one semantic grain per CTE/model. Add generic or singular tests for29 uniqueness, not-null, relationships, allowed values, reconciliation, and30 business invariants proportionate to consequence.315. Choose view/table/incremental/ephemeral from consumer, cost, rebuild, and32 lineage needs. An incremental model must define cutoff, late-arrival,33 updates/deletes, unique key, schema change, and full-refresh behavior.346. Compile first and inspect target SQL. Run the narrow selector in a safe35 target, then tests; use `dbt build` when ordered model/seed/snapshot/test36 execution is intended.377. Inspect `manifest.json`, `run_results.json`, relation row/grain checks, and38 warehouse query behavior before declaring completion.3940## Invariants4142- `ref()` establishes graph order but does not prove join cardinality or model43 grain.44- Tests are executable contracts, not documentation decoration.45- Incremental correctness includes late and changed records; a fast append-only46 query is wrong when source semantics permit updates.47- Macros generate SQL and can hide dialect behavior. Inspect compiled SQL and48 test every supported adapter branch.49- Snapshots model change history; define check/timestamp strategy and invalidate50 hard deletes deliberately.5152```sql53with orders as (54 select * from {{ ref('stg_orders') }}55),56customer_month as (57 select customer_id, date_trunc('month', ordered_at) as month,58 sum(net_revenue) as net_revenue59 from orders60 group by 1, 261)62select * from customer_month63```6465Read [graph and model design](references/models.md), [incremental and snapshot66semantics](references/stateful.md), and [compile/run verification](references/verification.md).