This document contains the automatically generated Entity Relationship Diagram for the FastAPI Template database schema. The ERD is generated from SQLModel definitions and updated automatically via git pre-commit hooks.
The ERD below shows the current database schema with all tables, fields, relationships, and constraints. This diagram is automatically maintained and reflects the actual SQLModel definitions in the codebase.
The database implements Row-Level Security (RLS) for automatic data isolation. Models that inherit from UserScopedBase automatically have:
- owner_id field: Foreign key to user.id for data ownership
- RLS policies: Automatically generated and applied during migrations
- User isolation: Users can only access their own data at the database level
- Admin bypass: Superusers can access all data through RLS policies
Models marked with 🔒 in the ERD are RLS-scoped and have automatic user isolation:
- User: Base model for authentication and user management
- Item: Example RLS-scoped model demonstrating user-owned data
Each RLS-scoped model has the following policies automatically applied:
- SELECT: Users can only see records where
owner_idmatches their user ID - INSERT: Users can only insert records with their own
owner_id - UPDATE: Users can only update records they own
- DELETE: Users can only delete records they own
The system uses multiple database roles for security:
- Application User (
rls_app_user): Normal application operations (subject to RLS) - Maintenance Admin (
rls_maintenance_admin): Maintenance operations (bypasses RLS)
%% This diagram is automatically generated from SQLModel definitions
%% Last updated: 2024-12-19
%% Generated by: ERD Generator v1.0
%% RLS: Row-Level Security enabled for user-owned models
erDiagram
USER {
uuid id PK
string email UK
string hashed_password
boolean is_active
boolean is_superuser
string full_name
datetime created_at
datetime updated_at
}
ITEM {
uuid id PK
uuid owner_id FK NOT NULL 🔒
string title
string description
datetime created_at
datetime updated_at
}
USER ||--o{ ITEM : "owns items (RLS enforced)"
- Purpose: Stores user account information
- Primary Key:
id(UUID) - Fields:
id: Primary key (UUID, auto-generated)hashed_password: User's hashed password (string)
- Purpose: Stores user-owned items
- Primary Key:
id(UUID) - Foreign Keys:
owner_id→USER.id
- Fields:
id: Primary key (UUID, auto-generated)owner_id: Foreign key to USER table (UUID, required)
- Type: One-to-Many
- Description: A user can own multiple items
- Implementation: Foreign key
owner_idin ITEM table - Cascade: Not specified (default behavior)
This ERD diagram is automatically updated whenever:
- SQLModel definitions are modified in
backend/app/models.py - New models are added or removed
- Relationships between models change
- Field definitions are updated
The ERD is updated via a git pre-commit hook that:
- Detects changes to SQLModel files
- Regenerates the ERD from current model definitions
- Updates this documentation file
- Validates the generated ERD syntax
You can manually regenerate the ERD using:
# Generate ERD from current models
python -m backend.scripts.generate_erd
# Generate with validation
python -m backend.scripts.generate_erd --validate --verbose
# Generate to custom location
python -m backend.scripts.generate_erd --output-path custom/erd.mmdThe ERD generation process includes validation to ensure:
- All SQLModel classes with
table=Trueare included - Primary keys are properly defined
- Foreign key relationships are valid
- Generated Mermaid syntax is correct
- All entities have at least one field
- Model Validation: Ensures all models have required fields and valid relationships
- Syntax Validation: Validates generated Mermaid ERD syntax
- Relationship Validation: Ensures all relationships reference valid entities
- Constraint Validation: Verifies database constraints are properly represented
The ERD generation system is designed to handle:
- Small schemas (< 5 tables): < 1 second
- Medium schemas (5-10 tables): < 5 seconds
- Large schemas (10-20 tables): < 30 seconds
- Very large schemas (20+ tables): Scales linearly
- Cause: Pre-commit hook not installed or not running
- Solution: Run
pre-commit installand ensure hooks are enabled
- Cause: Malformed SQLModel definitions or relationship issues
- Solution: Run
python -m backend.scripts.generate_erd --validateto identify issues
- Cause: SQLModel class missing
table=Trueparameter - Solution: Ensure all database models have
table=Truein their class definition
- Cause: Incorrect
back_populatesor foreign key definitions - Solution: Verify relationship definitions match between related models
If you encounter issues with the ERD generation:
-
Check validation output:
python -m backend.scripts.generate_erd --validate --verbose
-
Review model definitions in
backend/app/models.py -
Check pre-commit hook status:
pre-commit run erd-generation --verbose
-
Regenerate from scratch:
rm docs/database/erd.mmd python -m backend.scripts.generate_erd
- Model Discovery: Scan for SQLModel classes in specified paths
- Metadata Extraction: Parse SQLModel definitions to extract schema information
- Relationship Analysis: Detect and analyze relationships between models
- ERD Generation: Create Mermaid ERD syntax from extracted metadata
- Validation: Validate generated ERD for syntax and semantic correctness
- Output: Write ERD to documentation file
The ERD generator intelligently handles bidirectional relationships by:
- Detecting relationships with
back_populatesparameters - Showing only the one-to-many direction to reduce visual clutter
- Displaying foreign key fields to indicate reverse relationships
- Avoiding redundant relationship lines
- Input: SQLModel Python classes in
backend/app/models.py - Output: Mermaid ERD syntax in
docs/database/erd.mmd - Documentation: Markdown with embedded Mermaid in
docs/database/erd.md
This ERD system integrates with:
- Git Workflow: Automatic updates via pre-commit hooks
- Documentation: Part of project documentation standards
- CI/CD: Can be included in build and deployment pipelines
- Development: Provides real-time schema visualization
- Database Models - SQLModel definitions
- ERD Generator - Generation logic
- Quickstart Guide - Usage instructions
- Project Constitution - Documentation standards
This documentation is automatically maintained. Do not edit manually - changes will be overwritten.