A comprehensive example demonstrating how to use dbt-model-erd to automatically generate Entity Relationship Diagrams (ERDs) for your dbt models.
This example showcases:
- Automatic ERD Generation: Generate interactive ERD diagrams from dbt model relationships
- Best Practices: Follow standard data warehouse patterns (staging → prep → dw layers)
- Real-World Schema: E-commerce data model with customers, products, orders, and sales
- Relationship Detection: Automatically detect fact-dimension relationships using
ref()statements - Interactive Diagrams: View diagrams as interactive HTML files using Mermaid.js
dbt-erd/
├── models/
│ ├── prep/ # Staging/preparation layer
│ │ ├── dim/
│ │ │ ├── prep__dim_customer.sql
│ │ │ └── prep__dim_product.sql
│ │ └── fact/
│ │ └── prep__fact_order_items.sql
│ └── dw/ # Data warehouse layer
│ ├── dim/
│ │ ├── dim_customer.sql
│ │ ├── dim_product.sql
│ │ ├── dim_date.sql # Generated using date spine
│ │ └── schema.yml
│ └── fact/
│ ├── fact_orders.sql # Aggregated from order items
│ ├── fact_sales.sql # Line-item level sales
│ └── schema.yml
├── seeds/
│ ├── seed_customers.csv
│ ├── seed_products.csv
│ └── seed_order_items.csv
├── assets/img/ # Generated ERD diagrams
└── dbt_project.yml
- dim_customer: Customer master data with segments (Standard, Premium, VIP)
- dim_product: Product catalog with pricing, costs, and margin calculations
- dim_date: Date dimension from 1900-2100 generated using date spine logic
- fact_orders: Order-level aggregations (order totals, discounts, item counts)
- fact_sales: Line-item level sales with profit analysis
- Docker installed (that's it!)
# Clone the repo
git clone https://github.com/entechlog/dbt-examples.git
cd dbt-examples/dbt-erd
# Start the demo (builds, runs models, generates docs & ERDs, serves docs)
docker-compose upThat's it! The container will automatically:
- ✓ Install dbt packages
- ✓ Load seed data (customers, products, orders)
- ✓ Run all dbt models (prep → dw layers)
- ✓ Run dbt tests
- ✓ Generate ERD diagrams
- ✓ Generate dbt documentation
- ✓ Start dbt docs server on http://localhost:8080
dbt Documentation
- Open http://localhost:8080 in your browser
- Explore lineage graph, model definitions, and column descriptions
ERD Diagrams
- Open
assets/img/models/dw/fact/fact_orders_model.html - Open
assets/img/models/dw/fact/fact_sales_model.html - View relationships between fact and dimension tables
# Press Ctrl+C in the terminal, then:
docker-compose downShows relationships between:
dim_date→fact_sales(via sale_date_id)dim_product→fact_sales(via product_id)dim_customer→fact_sales(via customer_id)
Shows relationships between:
dim_date→fact_orders(via order_date_id)
- Scans SQL Files: Parses your dbt models to find
ref()statements - Reads Schema Files: Extracts column definitions and relationships from
schema.yml - Detects Relationships: Identifies foreign key relationships through:
- Column naming patterns (e.g.,
*_id,*_key) - Relationship tests in schema.yml
- Column naming patterns (e.g.,
- Generates Mermaid: Creates Mermaid ER diagrams with proper cardinality
- Creates HTML: Wraps diagrams in interactive HTML with Mermaid.js
- Updates Documentation: Adds diagram links to your schema.yml files
Create a custom erd_config.yml:
# Mermaid theme
theme: default # Options: default, neutral, forest, dark
# Diagram direction
direction: LR # LR (left-right) or TB (top-bottom)
# Column display
show_all_columns: true
max_columns: 10
# Output paths
output_dir: assets/img
mermaid_extension: .mmd
html_extension: .htmlThis example uses DuckDB - an embedded database that requires no separate installation or server. Everything runs inside Docker:
- No database setup needed - DuckDB is file-based (like SQLite)
- No configuration - profiles.yml points to local DuckDB file
- Fully isolated - Runs in Docker container
- Real dbt workflow - Actual seeds, models, tests, docs generation
- ERD generation - Automatic relationship detection from schema.yml
- Prep Layer: Standardization, deduplication using CTEs
- DW Layer: Final dimensional models with business logic
{{ dbt_utils.date_spine(
datepart="day",
start_date="cast('1900-01-01' as date)",
end_date="cast('2100-12-31' as date)"
) }}{{ dbt_utils.generate_surrogate_key(['customer_key']) }} AS customer_id- name: product_id
tests:
- relationships:
to: ref('dim_product')
field: product_id- dbt-model-erd Repository: https://github.com/entechlog/dbt-model-erd
- Main dbt Examples: https://github.com/entechlog/dbt-examples
- dbt Documentation: https://docs.getdbt.com/
- Mermaid.js: https://mermaid.js.org/
This example demonstrates:
- dbt-model-erd Usage - How to automatically generate ERDs from dbt models
- DuckDB with dbt - Using an embedded database for local development
- Docker Workflows - One-command setup for reproducible environments
- Data Modeling Patterns - Dimensional modeling (facts & dimensions)
- dbt Best Practices - Layered architecture, testing, documentation
- Extend the Model: Add more dimensions (stores, sales reps, regions)
- Add More Facts: Create shipping, inventory, or return fact tables
- Customize Diagrams: Modify erd_config.yml for different themes
- Integrate CI/CD: Add ERD generation to your deployment pipeline
- Embed in Docs: Include diagrams in dbt documentation
- Publish to Pages: Add to your existing GitHub Pages site as a subfolder
Found an issue or have a suggestion? Please open an issue in the dbt-model-erd repository.
This example is part of the dbt-examples repository and follows the same license.
Generated with ❤️ using dbt-model-erd