[v2 Bug] With static analysis off, a unit test can read a seed table before the seed has created it
- Dominant language
- Rust
- Stars
- 13.8k
- Forks
- 2.6k
- Avg merge
- 21h 31m
- Merged PRs (30d)
- 56
Description
### Is this a new bug in dbt v2.x compared to the latest version of dbt 1.x?
- [x] I believe this is a new bug in dbt v2.x
- [x] I have searched the existing issues and could not find a duplicate
### Current Behavior
When static analysis is off (globally, per node, or because the adapter has none, like ClickHouse), unit tests read the schema of each given relation from the database while they render (#13737).
If that given is a source() that points to a table created by a seed in the same run, nothing makes the unit test wait for the seed. On a fresh schema, dbt build can run the unit test's DESCRIBE before the seed's CREATE TABLE has finished:
```
Failed [ 1.56s] unit_test jaffle_shop_dbt_test__audit.test_does_location_opened_at_trunc_to_date
Skipped [-------] model jaffle_shop.stg_locations (view)
[error] [DbDriverFailed (dbt1308)]: Remote database error while fetching schema for unit test upstream relation from `given` '`raw`.`raw_stores`'
```
Seen with the jaffle-shop project on ClickHouse Cloud, where DDL takes about 1.5 s. Locally seeds finish in about 0.2 s, so the bug is usually hidden. Running the unit test again passes.
### Root Cause
`crates/dbt-tasks-sa/src/graph.rs` builds one Render task and one Run task per node. With static analysis off, a node's Render waits for its parents' **Run** tasks, because rendering may query the database. That covers a `given: ref('other_model')`:
```
other_model Run ──► model Render ──► unit test Render (OK: waits for Run)
```
A `source()` has no Run task. When a source is backed by a seed of the same run ("overlapping source"), `add_overlapping_source_seed_edges` connects the seed, but only **Run to Run**:
```
seed Run ──► model Run
model Render ──► unit test Render (BUG: nothing waits for seed Run)
```
The comment in that function says why: "Seed schemas are pre-registered before the graph, so only the Run→Run ordering matters here." That is true when static analysis is on (schemas come from the CSV). It is not true on the #13737 fallback path, where both the model render and the unit test render query the seed table.
So the fix for #13737 allowed the live schema fetch (`try_get_schema_from_cache` in `renderable/unit_test.rs`) but did not add the graph edge that makes it safe.
### Expected Behavior
- A unit test should never read a `given` relation from the database before the node that creates that relation in the same run has finished. This must hold for a `source()` that is backed by a seed of the run, the same way it already holds for a `given: ref('model')`.
- The result must not depend on how fast the warehouse runs DDL. The repro above (a seed with a 3 s pre-hook) should pass every time, and the jaffle-shop `dbt build` on ClickHouse Cloud should pass on the first run.
- When static analysis is on, nothing should change.
### Steps To Reproduce
Make the seed slow so the race always happens. The source must point at the seed's real schema.
```yaml
# dbt_project.yml
seeds:
repro:
+schema: raw_repro
raw_stores:
+pre_hook: "select sleep(3)"
# models/sources.yml
sources:
- name: ecom
schema: repro_raw_repro # _raw_repro
tables: [{name: raw_stores}]
# models/stg_locations.sql
select id as location_id, name as location_name, toDate(opened_at) as opened_date
from {{ source('ecom', 'raw_stores') }}
# models/unit_tests.yml
unit_tests:
- name: test_opened_date
model: stg_locations
given:
- input: source('ecom', 'raw_stores')
rows: [{id: 1, name: A, opened_at: "2020-01-02 10:00:00"}]
expect:
rows: [{location_id: 1, location_name: A, opened_date: "2020-01-02"}]
```
```
dbt build --static-analysis off # on a fresh schema
Failed [ 1.08s] unit_test repro_dbt_test__audit.test_opened_date
Succeeded [ 3.37s] seed repro_raw_repro.raw_stores (table)
[error] [DbDriverFailed (dbt1308)]: Remote database error while fetching schema for unit test upstream relation from `given` '`repro_raw_repro`.`raw_stores`'
```
In `dbt.log` the unit test starts rendering 2 ms after the seed starts running. With the fix it starts 3 s later, after the seed, and the build passes.
### Relevant log output
```shell
```
### Environment
```markdown
- OS: MacOS 26.6.2
- CPU: (x86 or ARM) ARM
- dbt distribution and version: (`dbt --version`): dbt-core 2.0.0-rc.1 (Compiled last 2026-09-04 using the commit `0c59d1e77`)
```
### Which database adapter are you using?
clickhouse
### Is this a discrepancy vs. dbt 1.x?
- [x] Yes — this works in dbt 1.x but not in dbt v2.x
### Additional Context
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.