Skip to main content

Generate entity-relationship diagrams for dbt models

Project description

📊 dbt-model-erd

Automatically generate beautiful entity-relationship diagrams for your dbt models

License: MIT Python 3.8+

🎯 What is dbt-model-erd?

dbt-model-erd analyzes your dbt SQL models and YAML schemas to automatically generate interactive ER diagrams showing relationships between fact and dimension tables. No manual diagramming required!

✨ Key Features

  • 🖼️ Visual Clarity: Generate clean, professional ER diagrams for your data warehouse
  • 🔄 Auto-Detection: Automatically detects relationships between fact and dimension tables
  • 📝 Documentation: Embeds diagrams directly in your dbt model documentation
  • 🧠 Smart Inference: Intelligently infers primary keys, foreign keys, and column types
  • 🛠️ Highly Configurable: Customize every aspect of your diagrams
  • 🔒 No External Dependencies: Renders diagrams client-side without requiring Node.js
  • 🏎️ CI/CD Friendly: Works seamlessly in continuous integration environments

🚀 Quick Start

Installation

pip install dbt-model-erd

Basic Usage

# Navigate to your dbt project
cd your-dbt-project

# Generate diagrams for fact models
python -m dbt_erd --model-path models/fact

# View the generated diagrams
open assets/img/fact/fact_orders_model.html

What It Does

  1. Scans your dbt SQL files for ref() relationships
  2. Reads YAML schemas for column definitions
  3. Generates Mermaid ER diagrams showing table structures
  4. Creates interactive HTML files
  5. Updates your model YAML with diagram links

👉 Complete Quick Start Guide


📖 Documentation

Document Description
QUICKSTART.md Get started in 5 minutes with examples
TESTING_GUIDE.md How to test locally before publishing
PUBLISHING_GUIDE.md Complete guide to publish to PyPI
CONTRIBUTING.md Guidelines for contributors

💡 Example

Your dbt Project

models/fact/fact_orders.sql:

WITH orders AS (
    SELECT * FROM {{ source('raw', 'orders') }}
),
customers AS (
    SELECT * FROM {{ ref('dim_customer') }}
),
products AS (
    SELECT * FROM {{ ref('dim_product') }}
),
final AS (
    SELECT
        o.order_id,
        o.customer_id,
        o.product_id,
        o.amount
    FROM orders o
    LEFT JOIN customers c ON o.customer_id = c.customer_id
    LEFT JOIN products p ON o.product_id = p.product_id
)
SELECT * FROM final

Generated Diagram

erDiagram
    fact_orders {
        int order_id PK
        int customer_id FK
        int product_id FK
        float amount
    }

    dim_customer {
        int customer_id PK
        string first_name
        string last_name
    }

    dim_product {
        int product_id PK
        string product_name
        float price
    }

    dim_customer ||--o{ fact_orders : "customer_id"
    dim_product ||--o{ fact_orders : "product_id"

🎨 Configuration

Generate Default Config

python -m dbt_erd --output-config my_config.yml

Example Configuration

# Visualization settings
visualization:
  max_dimensions: 10
  show_columns: true
  column_limit: 20

# Mermaid settings
mermaid:
  theme: "default"  # Options: default, neutral, forest, dark
  direction: "LR"   # Options: LR (left-right), TB (top-bottom)
  outputs:
    mmd: true   # Generate raw Mermaid source code
    html: true  # Generate HTML with client-side rendering

  # Table styling options
  style:
    fact_table_fill: "#f5f5f5"       # Light gray for fact tables
    dimension_table_fill: "#e8f4f8"  # Light blue for dimension tables

📁 See examples/ for more configuration options.


🔧 Advanced Usage

Parallel Processing

For large projects with many models:

python -m dbt_erd --model-path models/fact --parallel

Custom Configuration

python -m dbt_erd --model-path models/fact --config advanced_config.yml

Verbose Mode

For debugging:

python -m dbt_erd --model-path models/fact --verbose

CI/CD Integration

Example GitHub Actions workflow:

name: Update ERD Diagrams

on:
  push:
    branches: [ main ]
    paths:
      - 'models/**/*.sql'
      - 'models/**/*.yml'

jobs:
  update-diagrams:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.9'
      - name: Install dbt-model-erd
        run: pip install dbt-model-erd
      - name: Generate ERD diagrams
        run: python -m dbt_erd --model-path models/fact
      - name: Commit changes
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add assets/
          git commit -m "Update ERD diagrams" || echo "No changes"
          git push

👉 More CI/CD examples


🖼️ How It Works

Pure Browser-Based Rendering

dbt-model-erd generates diagrams without requiring Node.js or making external API calls:

  1. Mermaid Source Code (.mmd): Raw diagram definition you can version control
  2. HTML with Client-Side Rendering (.html): Interactive diagram rendered in the browser

When embedded in dbt docs, you get clickable HTML links to interactive diagrams.

Customizable Table Styling

You can customize the appearance of your diagrams:

  • Set different colors for fact and dimension tables
  • Choose from different themes (default, neutral, forest, dark)
  • Set diagram direction (left-right or top-bottom)
  • Control column display limits

Diagram Generation Process

  1. Analyzes SQL files to find fact and dimension tables based on naming patterns
  2. Detects relationships between tables by identifying foreign keys
  3. Generates Mermaid diagram code representing these relationships
  4. Creates HTML files that render the diagrams client-side
  5. Updates your model YAML files to include links to the diagrams in dbt docs

🤝 Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

Development Setup

git clone https://github.com/entechlog/dbt-model-erd.git
cd dbt-model-erd
pip install -e ".[dev]"

# Run tests
make test

# Run linting
make lint

# Format code
make format

📊 Project Status

  • Version: 0.1.0 (Beta)
  • Python: 3.8+
  • License: MIT
  • Status: Active Development

Roadmap

  • Support for bridge/junction tables
  • Enhanced relationship type detection
  • Export to PNG/SVG
  • Integration with dbt Cloud
  • Support for dbt metrics
  • Custom Jinja templates

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.


🙏 Acknowledgments

  • Built with Mermaid.js for diagram rendering
  • Inspired by the dbt community's need for better data model visualization
  • Thanks to all contributors

📧 Support & Community


⭐ Star History

If you find this tool helpful, please consider giving it a star on GitHub!

Star History Chart


Made with ❤️ for the dbt community

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dbt_model_erd-0.1.0.tar.gz (36.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dbt_model_erd-0.1.0-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file dbt_model_erd-0.1.0.tar.gz.

File metadata

  • Download URL: dbt_model_erd-0.1.0.tar.gz
  • Upload date:
  • Size: 36.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for dbt_model_erd-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c224c092af9d0e4b20e79a9a7d500ad10183439fc5693f8c04acc66c135623c0
MD5 f5b58098465ae3c360330b95f35b65ec
BLAKE2b-256 ccc4fc4ee1e0140a955e7ba8dc48b1439cb2dabfe710118e7baa7f21f8b96964

See more details on using hashes here.

File details

Details for the file dbt_model_erd-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dbt_model_erd-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for dbt_model_erd-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c5e04e01874e120d6d38673ffe9d02aa155024b9042089b65daf2f2aadac8eb8
MD5 692dd280c8aec619aca69befed5131a2
BLAKE2b-256 6c63b5604a170733e4b08344a8ba4c52cbcdc8aea741ead1b617b66dc1bc2783

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page