dbt / Basic: project and model foundations / Lesson 7
Connect a source model and its data tests
Declare a source, build a small staging model, and explain what its key tests do and do not establish.
On this page
Lesson goals
- Connect a declared source to a staging model.
- Explain not-null and uniqueness tests on a key.
- Distinguish building selected resources from verifying source freshness.
Define the model contract
A staging model should state what one row represents and which source supplies it. In this example the intended grain is one order per order_id, with customer_id and amount passed through from a declared source. The source relation and column names are illustrative and require a compatible warehouse and dbt project. A staging model does not by itself prove that every source record is valid or current.
Reference the declared source
The SQL model uses source to express the dependency on shop.orders rather than hard-coding a physical relation. dbt resolves the source according to project configuration when compiling the model. The SQL does not deduplicate or repair invalid records; that is deliberate so the key tests can expose violations. Adapt identifiers, types, and permissions to the actual adapter and source contract before using it.
-- models/stg_orders.sql
select
order_id,
customer_id,
amount
from {{ source("shop", "orders") }}
source declares lineage to the configured source relation; it does not repair data.
Declare and interpret tests
The YAML declares the source and two tests on the model’s order_id. not_null detects missing key values and unique detects repeated key values in the tested relation. Together they are useful evidence for the stated key, but they do not validate amount, customer existence, business completeness, or source freshness. Configure a source database and schema as needed for the actual environment, and confirm the syntax matches the project’s supported dbt version.
version: 2
sources:
- name: shop
tables:
- name: orders
models:
- name: stg_orders
columns:
- name: order_id
data_tests:
- not_null
- unique
The tests check missing and duplicate order IDs, not every business rule.
Choose a controlled execution scope
A selected dbt build can build and test selected resources in dependency order, subject to the project and selection rules. Inspect selection before running against an owned development target, because building can write warehouse relations. Source freshness is a separate check with its own configuration and execution; a successful build does not establish that upstream data meets a freshness objective. Review failures rather than turning tests off to obtain a green run.
Verify with known data
Use an isolated fixture containing valid distinct IDs, then introduce a null ID and a duplicate ID in separate cases. Confirm the appropriate test reports a failure and that the valid case passes. Verify the source relation and compiled model point to the intended development environment before execution. Compare row counts and a small sample against the source, and record which checks ran; passing tests are limited to their declared assertions.
Common errors and fixes
A test passes but the source is stale
- Why it happens
- Key tests were treated as a freshness check.
- How to fix it
- Configure and run the appropriate source freshness check separately.
- Prevent it next time
- Record freshness and data-test outcomes as distinct evidence.
The wrong relation is queried
- Why it happens
- The source configuration points at an unintended database or schema.
- How to fix it
- Inspect the resolved relation and target before running.
- Prevent it next time
- Use isolated development credentials and review source configuration.
Duplicate orders inflate a downstream total
- Why it happens
- The one-row-per-order grain was assumed without checking the key.
- How to fix it
- Investigate duplicate IDs and correct the source or model contract.
- Prevent it next time
- Retain a uniqueness test and controlled duplicate fixture.
2 practice exercises
1. What do the two tests establish about order_id?
Show worked solution
For the tested relation and run, they check that order_id is not null and does not repeat. They do not establish freshness, valid amounts, or referential integrity.
2. Why should a development target be checked before dbt build?
Show worked solution
The command can create or modify warehouse relations. Confirm selection, target, permissions, and expected effects before running, and review both model and test results afterward.
Key takeaways
- Declare lineage and model grain explicitly.
- Treat key tests, source freshness, and business validation as separate checks.