An open API service indexing awesome lists of open source software.

https://github.com/datnguye/dbterd

Generate the ERD as a code from dbt artifacts
https://github.com/datnguye/dbterd

cli d2 dbml dbt-cloud dbt-core drawdb erd graphviz mermaid plantuml python-api

Last synced: 19 days ago
JSON representation

Generate the ERD as a code from dbt artifacts

Awesome Lists containing this project

README

          



dbterd


Generate ERD-as-a-code from your dbt projects



dbterd logo

Transform your dbt artifact files or metadata into stunning Entity Relationship Diagrams using multiple formats: DBML, Mermaid, PlantUML, GraphViz, D2, and DrawDB

[![docs](https://img.shields.io/badge/docs-visit%20site-blue?style=flat&logo=gitbook&logoColor=white)](https://dbterd.datnguyen.de/)
[![PyPI version](https://badge.fury.io/py/dbterd.svg)](https://pypi.org/project/dbterd/)
![python-cli](https://img.shields.io/badge/CLI-Python-FFCE3E?labelColor=14354C&logo=python&logoColor=white)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![python](https://img.shields.io/badge/Python-3.9|3.10|3.11|3.12|3.13-3776AB.svg?style=flat&logo=python&logoColor=white)](https://www.python.org)
[![codecov](https://codecov.io/gh/datnguye/dbterd/graph/badge.svg?token=N7DMQBLH4P)](https://codecov.io/gh/datnguye/dbterd)

[![dbterd stars](https://img.shields.io/github/stars/datnguye/dbterd.svg?logo=github&style=for-the-badge&label=Star%20this%20repo)](https://github.com/datnguye/dbterd)

## ๐ŸŽฏ Entity Relationship Detection

dbterd intelligently detects entity relationships through three algorithms โ€” pick the one that matches how your dbt project expresses its data contracts:

- **๐Ÿงช [Test Relationships](https://docs.getdbt.com/reference/resource-properties/data-tests#relationships)** *(default)* โ€” infers relationships from dbt `relationships` data tests
- **๐Ÿ›๏ธ [Semantic Entities](https://docs.getdbt.com/docs/build/entities)** (`-a entity_relationship`) โ€” detects relationships via dbt Semantic Layer entity definitions
- **๐Ÿ“œ [Model Contract Constraints](https://docs.getdbt.com/docs/collaborate/govern/model-contracts)** (`-a model_contract`) โ€” detects relationships via dbt model contract's `foreign_key` constraints (dbt 1.9+ / manifest v12+)

For detailed configuration options, see our [CLI References](https://dbterd.datnguyen.de/latest/nav/guide/cli-references.html#dbterd-run-algo-a).

## ๐ŸŽจ Supported Output Formats

No need to pick just one โ€” dbterd has a format for every occasion, from quick GitHub previews to full-blown interactive database designers.

| Format | Description | Use Case |
|--------|-------------|----------|
| **[DBML](https://dbdiagram.io/d)** | Database Markup Language | Interactive web diagrams |
| **[Mermaid](https://mermaid-js.github.io/mermaid-live-editor/)** | Markdown-friendly diagrams | Documentation, GitHub |
| **[PlantUML](https://plantuml.com/ie-diagram)** | Text-based UML | Technical documentation |
| **[GraphViz](https://graphviz.org/)** | DOT graph description | Complex relationship visualization |
| **[D2](https://d2lang.com/)** | Modern diagram scripting | Beautiful, customizable diagrams |
| **[DrawDB](https://drawdb.vercel.app/)** | Web-based database designer | Interactive database design |

๐ŸŽฏ **[Try the Quick Demo](https://dbterd.datnguyen.de/latest/nav/guide/targets/generate-dbml.html)** with DBML format!

## ๐Ÿš€ Installation

```bash
pip install dbterd --upgrade
```

Verify Installation:

```bash
dbterd --version
```

> [!TIP]
> **For dbt-core users**: It's highly recommended to keep [`dbt-artifacts-parser`](https://github.com/yu-iskw/dbt-artifacts-parser) updated to the latest version to support newer `dbt-core` versions and their [manifest/catalog json schemas](https://schemas.getdbt.com/):
> ```bash
> pip install dbt-artifacts-parser --upgrade
> ```
>
> **Note**: `dbterd` now automatically bypasses Pydantic validation errors by default, which helps with compatibility when using newer dbt artifact schemas.

## โš™๏ธ Configuration Files

Tired of typing the same CLI arguments repeatedly? Your fingers deserve better. `dbterd` supports configuration files to streamline your workflow!

```bash
# Initialize a configuration file
dbterd init

# Now just run with your saved settings
dbterd run
```

**Supported formats:**
- `.dbterd.yml` - YAML configuration (recommended)
- `pyproject.toml` - Add `[tool.dbterd]` section to your existing Python project config

Learn more in the [Configuration Files Guide](https://dbterd.datnguyen.de/latest/nav/guide/configuration-file.html).

## ๐Ÿ’ก Examples

### CLI Examples

๐Ÿ–ฑ๏ธ Click to explore CLI examples

```bash
# ๐Ÿ“Š Select all models in dbt_resto
dbterd run -ad samples/dbtresto

# ๐ŸŽฏ Select multiple dbt resources (models + sources)
dbterd run -ad samples/dbtresto -rt model -rt source

# ๐Ÿ” Select models excluding staging
dbterd run -ad samples/dbtresto -s model.dbt_resto -ns model.dbt_resto.staging

# ๐Ÿ“‹ Select by schema name
dbterd run -ad samples/dbtresto -s schema:mart -ns model.dbt_resto.staging

# ๐Ÿท๏ธ Select by full schema name
dbterd run -ad samples/dbtresto -s schema:dbt.mart -ns model.dbt_resto.staging

# ๐ŸŒŸ Other sample projects
dbterd run -ad samples/fivetranlog -rt model -rt source
dbterd run -ad samples/facebookad -rt model -rt source
dbterd run -ad samples/shopify -s wildcard:*shopify.shopify__*

# ๐Ÿ”— Custom relationship detection
dbterd run -ad samples/dbt-constraints -a "test_relationship:(name:foreign_key|c_from:fk_column_name|c_to:pk_column_name)"

# ๐Ÿ’ป Your local project
dbterd run -ad samples/local -rt model -rt source
```

### Python API Examples

**Generate Complete ERD**

```python
from dbterd.api import DbtErd

# Generate DBML format
erd = DbtErd().get_erd()
print("ERD (DBML):", erd)

# Generate Mermaid format
erd = DbtErd(target="mermaid").get_erd()
print("ERD (Mermaid):", erd)
```

**Generate Single Model ERD**

```python
from dbterd.api import DbtErd

# Get ERD for specific model
dim_prize_erd = DbtErd(target="mermaid").get_model_erd(
node_unique_id="model.dbt_resto.dim_prize"
)
print("ERD of dim_prize (Mermaid):", dim_prize_erd)
```

**Sample Output:**

```mermaid
erDiagram
"MODEL.DBT_RESTO.DIM_PRIZE" {
varchar prize_key
nvarchar prize_name
int prize_order
}
"MODEL.DBT_RESTO.FACT_RESULT" {
varchar fact_result_key
varchar box_key
varchar prize_key
date date_key
int no_of_won
float prize_value
float prize_paid
int is_prize_taken
}
"MODEL.DBT_RESTO.FACT_RESULT" }|--|| "MODEL.DBT_RESTO.DIM_PRIZE": prize_key
```

---

## ๐Ÿค Contributing

We welcome contributions! Whether you've found a bug, dreamed up a feature, or just want to fix a typo โ€” you're very welcome here.

**Ways to contribute:** ๐Ÿ› Report bugs | ๐Ÿ’ก Suggest features | ๐Ÿ“ Improve documentation | ๐Ÿ”ง Submit pull requests

See our **[Contributing Guide](https://dbterd.datnguyen.de/latest/nav/development/contributing-guide.html)** for detailed information.

**Show your support:**
- โญ Star this repository
- ๐Ÿ“ข Share on social media
- โœ๏ธ Write a blog post
- โ˜• [Buy me a coffee](https://www.buymeacoffee.com/datnguye)

[![buy me a coffee](https://img.shields.io/badge/buy%20me%20a%20coffee-donate-yellow.svg?logo=buy-me-a-coffee&logoColor=white&labelColor=ff813f&style=for-the-badge)](https://www.buymeacoffee.com/datnguye)

## ๐Ÿ‘ฅ Contributors

A huge thanks to our amazing contributors โ€” the people who turned "wouldn't it be nice if..." into actual working code. ๐Ÿ™



## ๐Ÿ“ง Support

**Need help?** We're here for you! Check ๐Ÿ“– [Documentation](https://dbterd.datnguyen.de/), ๐Ÿ› [Report Issues](https://github.com/datnguye/dbterd/issues) and ๐Ÿ’ฌ [Discussions](https://github.com/datnguye/dbterd/discussions)





Star History Chart

---

**Made with โค๏ธ by the dbterd community**

---

### Sponsored by GitAds
[![Sponsored by GitAds](https://gitads.dev/v1/ad-serve?source=datnguye/dbterd@github)](https://gitads.dev/v1/ad-track?source=datnguye/dbterd@github)