how to fix dbt error "database error: relation staging table does not exist" during test run
Create the staging relation before the test run, in the right schema: the test queried a staging table that the run never built, usually because the test ran standalone, the model was deselected, or the schema macro resolves differently under the test target. Use when a dbt project running tests against a target where the staging model may not have been built. Not for a production incident where the staging table
TL;DR
Create the staging relation before the test run, in the right schema: the test queried a staging table that the run never built, usually because the test ran standalone, the model was deselected, or the schema macro resolves differently under the test target. A test cannot create its model. Running dbt test alone against a target where the staging model was never materialised produces exactly this database error.
how to fix dbt error "database error: relation staging table does not exist" during test runUse this when
- The check or run fails with this symptom: a dbt run fails with database error relation staging table does not exist during a test run.
- You are working on a dbt project running tests against a target where the staging model may not have been built and the failure repeats on the same data or configuration.
- You need the failing rows, rule, or stage isolated before changing thresholds or code.
Not for this skill when
- A production incident where the staging table was dropped by another process, which needs ownership and permissions work rather than a build order fix.
- The error text names a different tool, stage, or root cause. Match the verbatim error first and use that skill.
Steps
- Check whether the staging relation exists in the target schema the test ran against. Verify: the relation is absent in that schema, or present under a different schema name
- Review the command that ran: a test-only invocation, a selection that excluded the model, or a fresh target. Verify: the reason the model was never built is identified
- Run dbt build or dbt run for the staging model and its parents in the same target. Verify: the relation exists with current data
- Rerun the tests in the same invocation pattern the pipeline uses. Verify: tests pass against the built relation
- Make the pipeline entry point a build or a run-then-test sequence so the standalone test case cannot recur. Verify: the job definition shows the ordered steps
Variant phrasings
dbt database error relation staging table does not exist test
The shorter phrasing focused on the named failure.
dbt test relation does not exist staging
The symptom phrasing a searcher tries next.
dbt staging table missing during test run fix
The variant naming the failing stage or tool directly.
Compatibility: dbt Core 1.5 and later, dbt build and test selection behaviour
Why it happens
Relation existence depends on target, schema macros, and what ran before. Ephemeral models, custom schema names, and test-only CI steps against empty schemas all produce a missing relation that looks like a data problem but is a build order problem.
Edge cases / pitfalls
- Custom generateschemaname macros often resolve differently for the default target and CI targets; compare the compiled relation name across targets.
- An ephemeral model has no relation at all; a test or a downstream query that expects a physical table needs the materialisation changed.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_p1Ot7UDviNAjdjW7PbbqXA