Working with Sources
Sources in dbt represent the raw data tables in your warehouse that serve as the foundation for your transformations. Rather than referencing raw tables directly, dbt allows you to define sources in a centralized way, improving maintainability and enabling powerful features like freshness checking.
What Are Sources?
In dbt, sources represent raw data tables from external systems, such as an operational database, CRM, or third-party APIs. Instead of referencing raw tables directly in models, dbt allows you to define sources in a centralized file (sources.yml
) for better organization, maintainability, and documentation.
The sources.yml
file is a crucial component in dbt projects, centralizing metadata about raw data tables. This ensures consistency, maintainability, and automatic documentation.
Why Use Sources?
Centralizes raw table definitions – Avoids hardcoded table names across multiple models.
Improves maintainability – If raw table locations change, you only need to update
sources.yml
.Enables freshness checks – dbt can monitor source data latency.
Enhances documentation – Automatically generates lineage graphs and model dependencies.
Defining Sources in YAML
Sources are defined in .yml
files under the sources:
key. Here's a typical example:
In this example:
We've defined a source named
jaffle_shop
that points to tables in theraw.jaffle_shop
schemaWe've defined two tables:
orders
andcustomers
We've added column-level tests to the
orders
table
Using Sources in Models
Once sources are defined, you can reference them using the source()
function in your dbt models:
This offers several advantages:
Consistency: Source references are standardized across your project
Refactoring: If a source table moves, you only need to update one place
Documentation: dbt automatically builds lineage from sources to models
Testing: You can apply tests to sources for early validation
Best Practices for Source Organization
Group Related Sources
Organize sources by system or domain:
Document Your Sources
Add descriptions to help your team understand the data:
Apply Tests to Sources
Find data quality issues early by testing your sources:
Source Freshness
One of the most powerful features of sources is the ability to check data freshness - ensuring your source data is up-to-date before you build models on top of it.
Configuring Freshness Checks
Add a freshness
block and specify a loaded_at_field
in your sources definition:
This configuration:
Uses the
updated_at
column to determine when data was last loadedWarns if data is more than 12 hours old
Errors if data is more than 24 hours old
Running Freshness Checks
Check freshness with:
The output will show the status of each source:
Table-Specific Freshness
You can override source-level freshness settings for specific tables:
In this example, real_time_stock
has stricter freshness requirements than other tables in the source.
Advanced Source Configurations
Source Overrides by Environment
You can override source details for different environments by using custom schemas:
Filtering Source Data
For large source tables, you can define filter conditions:
Generating Source Definitions
If you're using Paradime, you can automatically generate source definitions:
This command:
Scans your data warehouse
Identifies tables in raw schemas
Generates
sources.yml
files with the correct structure
Paradime Source Generation Benefits
✅ Scans your data warehouse and auto-generates the correct table definitions. ✅ Prevents manual errors in source definitions. ✅ Keeps sources up to date with your evolving data warehouse schema.
Common Source Patterns
Staging Models for Sources
A common pattern is to create staging models that select from sources. These provide a clean interface between raw data and your transformations:
Testing Complex Source Relationships
You can test relationships between source tables:
By effectively managing sources in dbt, you build a strong foundation for your analytics pipeline, making it easier to maintain, test, and document the origin of your data.
Last updated
Was this helpful?