{"record":{"id":"b06fa6d7dfa0e5d9","repo":"dbt-labs/dbt-core","slug":"unit-test-must-have-a-dependency","errorCode":null,"errorMessage":"Unit test must have a dependency","messagePattern":"Unit test must have a dependency","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/dbt-jinja-utils/src/phases/compile/compile_node_context.rs","lineNumber":190,"sourceCode":"            .as_object()\n            .unwrap()\n            .downcast_ref::<BTreeMap<String, MinijinjaValue>>()\n            .unwrap()\n            .clone()\n    } else {\n        BTreeMap::new()\n    };\n    let mut ctx = base_context.clone();\n\n    let this_relation = match model.resource_type() {\n        NodeType::UnitTest => {\n            let ref_name = model\n                .base()\n                .refs\n                .first()\n                .cloned()\n                .map(|r| r.name)\n                .expect(\"Unit test must have a dependency\");\n            let (_, this_relation, _, _) = node_resolver.lookup_ref(\n                &Some(model.common().package_name.clone()),\n                &ref_name,\n                &None,\n                &None,\n            )?;\n            this_relation\n        }\n        NodeType::Model => {\n            let ref_name = model.common().name.clone();\n            // for repl, we use the just create a relation on spot using model passed in.\n            if ref_name == REPL_MODEL_NAME {\n                dbt_adapter::relation::RelationObject::new(Arc::from(\n                    dbt_adapter::relation::do_create_relation(\n                        adapter_type,\n                        model.base().database.clone(),\n                        model.base().schema.clone(),\n                        Some(model.base().alias.clone()),","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/dbt-labs/dbt-core/blob/0267ce9170576975b76b64ce856b2e5848e96617/crates/dbt-jinja-utils/src/phases/compile/compile_node_context.rs#L172-L208","documentation":"While building the compile-node context for a unit test, the code takes the first `ref` of the test model and unwraps it with `expect`. dbt requires every unit test to declare at least one dependency (a ref to the model under test); if `refs` is empty the expect panics. This is an invariant the unit-test schema is supposed to guarantee.","triggerScenarios":"Running `dbt build`/compile where a unit test node has an empty `refs` list — e.g. a unit test YAML entry with no `given`/model ref, a unit test defined without `model: name:` input, or a partially parsed test node whose refs were not attached.","commonSituations":"Hand-written unit_tests blocks in schema YAML missing the `model` key; migrating tests from generic `tests:` to `unit_tests:` without adding depends-on refs; bugs in parsing that drop refs from unit test nodes.","solutions":["Add the required `model:` (and any `given` refs) to the unit test definition in your schema YAML so the node has at least one ref","Run `dbt parse`/`dbt ls --select <test>` to confirm the unit test node resolves with refs attached","Validate unit test definitions at parse time (fail with a proper error instead of panicking) when `refs` is empty","Upgrade dbt if you hit this with a structurally valid test — it may be a parser bug that drops refs"],"exampleFix":"// before (schema.yml)\nunit_tests:\n  - name: my_test\n    given: []\n// after\nunit_tests:\n  - name: my_test\n    model: my_model\n    given:\n      - input: ref('my_model')\n        rows: []","handlingStrategy":"validation","validationCode":"# dbt schema YAML check: unit test must declare a model/ref\n# before running builds, or in Rust before context build:\nif model.base().refs.is_empty() {\n    return Err(anyhow!(\"unit test '{}' has no refs; add a model/given ref\", model.common().name));\n}","typeGuard":null,"tryCatchPattern":"// surface as a normal error instead of panicking\nlet ref_name = model.base().refs.first().map(|r| r.name.clone())\n    .ok_or_else(|| anyhow::anyhow!(\"Unit test must have a dependency\"))?;","preventionTips":["Always include `model:` in unit_tests entries in schema YAML","Run `dbt parse` and inspect nodes before building","Lint schema YAML for unit tests missing model/given refs","Validate unit test nodes at parse time rather than at context build"],"tags":["panic","unit-test","missing-ref","jinja","compile"],"backgroundTag":"missing-required-argument","analyzedSha":"0267ce9170576975b76b64ce856b2e5848e96617","analyzedAt":"2026-09-07T21:53:39.732Z","contentChangedAt":"2026-09-07T21:53:39.732Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}