Understanding Relationships
Relationships define how tables connect to each other through foreign keys. ChartDB supports all standard relationship types and provides visual tools for managing them.Relationship Types
ChartDB supports four relationship cardinalities:One-to-One (1:1)
Each record in Table A relates to exactly one record in Table B.Example: User → UserProfile
One-to-Many (1:N)
Each record in Table A relates to multiple records in Table B.Example: Customer → Orders
Many-to-One (N:1)
Multiple records in Table A relate to one record in Table B.Example: Orders → Customer
Many-to-Many (N:M)
Records in both tables can relate to multiple records in the other table (requires junction table).Example: Students ↔ Courses
Creating Relationships
Method 1: Visual Connection (Recommended)
The fastest way to create relationships:Start Connection
Right-click on a table and select Add Relationship.A floating connection line will appear from the table.
Select Target Table
Click on the target table to complete the relationship.ChartDB will automatically select matching fields if available.
Method 2: Create Relationship Dialog
For more control over relationship creation:Relationship Properties
Unique identifier for the relationship.
Relationship name, typically follows the pattern:
fk_{source_table}_{target_table}{source_table}_ibfk_1(MySQL auto-generated)
dept_emp_ibfk_1The ID of the table containing the foreign key.
The ID of the table being referenced (usually contains the primary key).
The field in the source table (foreign key field).
The field in the target table (primary key or unique field).
Cardinality on the source side of the relationship.
Cardinality on the target side of the relationship.
Schema of the source table (for databases with schema support).
Schema of the target table (for databases with schema support).
Editing Relationships
Quick Edit via Popover
Click on any relationship line to open the edit popover:Edit Cardinality
The popover shows buttons to quickly change between:
- 1:1 (One-to-One)
- 1:N (One-to-Many)
Edit via Sidebar
For detailed relationship configuration:Switching Relationship Direction
You can reverse the source and target of a relationship:- Click on the relationship line
- Click the Switch button (⇄) in the popover
- The source and target tables/fields are swapped
Deleting Relationships
To remove a relationship:- Via Popover
- Via Table Deletion
- Click on the relationship line
- Click the delete button (trash icon) in the popover
- Confirm deletion
Relationship Visualization
Visual Elements
Relationships are displayed as connecting lines with visual indicators:Line Style
- Solid lines for foreign key relationships
- Arrow heads indicate direction
- Hover to highlight
Cardinality Markers
- Circle (◯) = “one”
- Crow’s foot (⋮) = “many”
- Positioned at each end of the line
Connection Points
Relationships connect at the specific fields involved:- The line extends from the foreign key field
- To the referenced primary key field
- Labels show field names on hover
Common Relationship Patterns
One-to-Many: Customer → Orders
One-to-One: User → Profile
For one-to-one relationships, add a UNIQUE constraint on the foreign key field.
Many-to-Many: Students ↔ Courses
Self-Referencing: Employee → Manager
Self-referencing relationships point from a table back to itself. Useful for hierarchical data like org charts, category trees, etc.
Composite Foreign Keys
For tables with composite primary keys:Cross-Schema Relationships
For databases supporting schemas (PostgreSQL, SQL Server):sourceSchemaandsourceTableIdtargetSchemaandtargetTableId
Referential Actions
While creating relationships, you can specify what happens on UPDATE and DELETE:ON DELETE Actions
ON DELETE Actions
- CASCADE: Delete related rows
- SET NULL: Set foreign key to NULL
- SET DEFAULT: Set to default value
- RESTRICT: Prevent deletion
- NO ACTION: Similar to RESTRICT
ON UPDATE Actions
ON UPDATE Actions
- CASCADE: Update related rows
- SET NULL: Set foreign key to NULL
- SET DEFAULT: Set to default value
- RESTRICT: Prevent update
- NO ACTION: Similar to RESTRICT
Referential action support varies by database. ChartDB exports the appropriate syntax for your selected database type.
Best Practices
Foreign Key Naming
Foreign Key Naming
Use consistent naming conventions:
{referenced_table}_idfor the field namefk_{source_table}_{target_table}for constraint name
- Field:
customer_id, Constraint:fk_orders_customers - Field:
user_id, Constraint:fk_posts_users
Index Foreign Keys
Index Foreign Keys
Always index foreign key fields for performance:
- Speeds up joins
- Required for some databases
- ChartDB exports appropriate indexes
Choose Correct Cardinality
Choose Correct Cardinality
Think carefully about relationship types:
- One-to-Many is most common
- One-to-One needs UNIQUE constraint on FK
- Many-to-Many requires junction table
Document Relationships
Document Relationships
Add comments explaining:
- Why tables are related
- Business rules enforced
- Special cascade behaviors
Avoid Circular Dependencies
Avoid Circular Dependencies
Be cautious of circular references:
- Can cause issues with data loading
- May need to temporarily disable constraints
- Document any circular dependencies clearly
Troubleshooting
Relationship Won't Create
Relationship Won't Create
Common causes:
- Data types don’t match
- Target field is not a primary key or unique
- Schema mismatch
Relationship Line Not Visible
Relationship Line Not Visible
Possible reasons:
- Tables are overlapping
- Canvas is zoomed too far out
- Relationship is filtered out
Can't Delete Relationship
Can't Delete Relationship
Issue: Diagram is in readonly mode.Solution: Ensure you have edit permissions and the diagram is not locked.
Next Steps
Editing Schema
Learn how to create and modify tables
Using Areas
Group related tables visually
Export Schema
Export your schema to SQL, DBML, or images
Keyboard Shortcuts
Speed up relationship management
