Updated for nself v0.4.8
One of the most powerful features of nself's database management is its seamless integration with dbdiagram.io, a popular visual database design tool. This integration enables a complete visual-to-code workflow, allowing you to design your database schema visually and synchronize it with your nself project.
dbdiagram.io is a free, simple tool to draw entity-relationship (ER) diagrams by just writing code. Created by Holistics (the same team behind DBML), it provides:
The nself + dbdiagram.io integration provides:
Design your schema visually at dbdiagram.io, then import:
# Import DBML and create SQL migration
nself db schema import schema.dbml
# This creates:
# - nself/migrations/20260122_imported_schema.up.sql
# - nself/migrations/20260122_imported_schema.down.sqlGenerate DBML from your existing database:
# Export current database schema to DBML
nself db schema diagram > schema.dbml
# Open the generated file at dbdiagram.io to visualize# Apply everything: import -> migrate -> seed
nself db schema apply schema.dbml
# This automatically:
# 1. Imports DBML -> creates SQL migration
# 2. Runs migration
# 3. Generates mock data (local/staging only)
# 4. Seeds sample usersnself v0.4.8 provides pre-built schema templates to get started quickly:
# Create starter schema from template
nself db schema scaffold basic # Users, profiles, posts
nself db schema scaffold ecommerce # Products, orders, cart
nself db schema scaffold saas # Organizations, members, projects
nself db schema scaffold blog # Posts, categories, comments
# Each creates a schema.dbml file you can:
# 1. Open at dbdiagram.io for visual editing
# 2. Customize to your needs
# 3. Apply with: nself db schema apply schema.dbmlnself db schema import schema.dbmlnself db migrate upnself db schema diagram > schema.dbmlCompare your local schema with another environment:
nself db schema diff staging
# Example output:
# Schema Differences (local vs staging)
# ────────────────────────────────────
# + Table: user_preferences (missing in staging)
# ~ Column: users.last_login (different type)
# - Index: idx_orders_date (missing in local)Get index suggestions based on your schema:
nself db schema indexes
# Example output:
# Index Recommendations
# ─────────────────────
# [HIGH] orders.user_id - Frequently joined, no index
# [MEDIUM] products.category_id - Used in WHERE clauses
# [LOW] users.created_at - Occasional range queriesKeep your visual designs versioned alongside your code:
# Export and commit diagram snapshots
nself db schema diagram > schema.dbml
git add schema.dbml
git commit -m "Update schema diagram"# Problem: Import fails with parse error
# Solution: Check DBML syntax:
# - Column names must be valid identifiers
# - Types must be supported
# - Brackets must be balanced
# Tip: Validate at dbdiagram.io before importing# Check status and repair if needed
nself db migrate status
nself db migrate repair # Fix tracking table# Schema commands (v0.4.8)
nself db schema scaffold basic # Create from template
nself db schema import file.dbml # DBML to SQL migration
nself db schema apply file.dbml # Full workflow: import + migrate + seed
nself db schema diagram # Database to DBML
nself db schema diff staging # Compare schemas
nself db schema indexes # Suggest indexes
# After changes in dbdiagram.io:
# 1. Export DBML from dbdiagram.io
# 2. nself db schema import schema.dbml --name my_changes
# 3. nself db migrate upNow that you understand dbdiagram.io integration:
The dbdiagram.io integration makes database design a collaborative, visual process while maintaining all the benefits of code-based schema management.