01. Defining Sources
What Are Sources in DBT?
Sources allow you to reference raw data tables in your warehouse using a centralized YAML definition instead of hardcoding schema and table names in SQL.
Why Use Sources?
- Avoid hardcoding database names in models
- Track lineage from raw tables to models
- Apply freshness checks for raw data
- Improve maintainability across environments
How to Define a Source?
Create a YAML file (e.g., src_orders.yml) in your models/ or sources/ folder:
version: 2
sources:
- name: raw
database: analytics
schema: raw_data
tables:
- name: orders
- name: customers
Referencing Sources in Models
Instead of writing:
select * from analytics.raw_data.orders
Use:
select * from {{ source('raw', 'orders') }}
Quick note: What does {{ }} mean?
{{ ... }}is dbt’s template syntax. It is not native SQL.- dbt evaluates the expression inside the braces before sending SQL to Snowflake.
source('raw', 'orders')looks up the source definition in YAML and returns the real table name.- In Snowflake, dbt compiles this into something like:
select * from analytics.raw_data.orders
So in simple terms: {{ source(...) }} is a dbt shortcut that replaces hardcoded table names with the correct warehouse reference at compile time.
Adding Descriptions and Tests
Enhance your YAML with metadata:
version: 2
sources:
- name: raw
description: "Raw data sources from the upstream system"
tables:
- name: orders
description: "Order details table"
columns:
- name: order_id
description: "Primary key"
tests:
- not_null
- unique
Applying Freshness Checks
In dbt_project.yml:
freshness:
warn_after: {count: 24, period: hour}
error_after: {count: 48, period: hour}
Run:
dbt source freshness
Best Practices for Sources
Define one source file per schema Use descriptive names for sources and tables Add tests for critical columns Schedule freshness checks in production
Summary
Sources help you standardize raw data references, enable data quality checks, and improve lineage visibility in DBT Docs.