dbt: 'source' is undefined in staging model"
Fixes the dbt source-is-undefined error by declaring the source in a _sources.yml file and calling it with the correct two-argument source() form. Use when a staging model fails with 'source' is undefined. Not for ref() errors, which mean a model name problem instead.
TL;DR
The model calls source('raw', 'orders') but dbt has no such source declared. Define it in a _sources.yml file under your model paths with the right database, schema, and table, then recompile. The source() function only knows about declared sources.
Error
"dbt: 'source' is undefined in staging model"Steps
- Find the
source()call in the staging model and note both arguments: source name and table name. Expected: you know exactly what dbt is looking for. - Check for a
_sources.yml(orsources.yml) file under your model paths that declares that source. Expected: you find it missing or misspelled. - Add or fix the declaration:
version: 2
sources:
- name: raw
database: [YOUR DATABASE]
schema: [YOUR SCHEMA]
tables:
- name: ordersExpected: the source name and table name match the source() call exactly.
- Run
dbt ls --resource-type source --select source:raw.orders. Expected: dbt lists the source. - Run
dbt compile --select [STAGING MODEL], thendbt run --select [STAGING MODEL]. Expected: the model builds.
When to use
- A model fails with "'source' is undefined".
- You just added a new raw table reference to a staging model.
When not to use
- The error is "'ref' is undefined" (a model-name problem, not a source problem).
- The source is declared but the table name is wrong (fix the table entry).
Tool compatibility
- dbt Core 1.0 and later, all adapters. Source declarations are adapter-independent.
Variant phrasings
Source 'raw.orders' not found
The same problem phrased as a lookup failure; the declaration is still the fix.
source() called with one argument
source() always takes two arguments (source name, table name); a single argument is a different error.
Why it happens
Unlike ref(), which resolves against model files, source() resolves against declared source definitions. An undeclared source is invisible to dbt, so the function has nothing to resolve.
Edge cases
- The
_sources.ymlfile must live under a configuredmodel-paths(orsource-paths) directory, or dbt never parses it. - Database and schema in the declaration must match the warehouse exactly, including case on Snowflake.
- Freshness checks also read these declarations; a bad declaration breaks
dbt source freshnesstoo.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_7plPJvPnEBNJjG4bqgrf5w
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.