BackendDatabase
Data Model
Complete entity-relationship overview of all Django models.
Entity Relationships
User
├── owns → Workspace (via owner FK)
├── member of → Workspace (via WorkspaceMembership M2M)
├── has one → StripeUser → Subscription → SubscriptionItem → Price → Product
├── has one → UserCost (cumulative AI spend tracking)
└── has many → AIUsage (per-call cost logs)
Workspace
├── has many → ChatSession
├── has many → KnowledgeBase
├── has many → Document
├── has many → Team → TeamMembership
├── has many → Invitation
├── has many → Folder
├── has one → Subscription (FK)
└── has many → WorkspaceCost, AIUsage
ChatSession
├── has many → Message
├── has many → ChatSessionShare
└── attached to → KnowledgeBase (via ChatSessionKnowledgeBase M2M)
KnowledgeBase
├── has many → Document (via KnowledgeBaseDocument M2M)
└── has many → KnowledgeBaseTeamAccess
Document
├── has many → DocumentChunk (text + embedding vector)
└── has many → DocumentProcessingTaskModel Summary
| App | Model | Primary Key | Key Relationships |
|---|---|---|---|
| users | User | BigAutoField | email (unique), USERNAME_FIELD |
| users | UserCost | BigAutoField | OneToOne → User |
| workspaces | Workspace | UUID | owner → User, subscription → Subscription |
| workspaces | WorkspaceMembership | BigAutoField | workspace + user (unique together) |
| workspaces | Team | BigAutoField | workspace → Workspace, owner → User |
| workspaces | TeamMembership | BigAutoField | team + user (unique together) |
| workspaces | Invitation | BigAutoField | workspace/team → inviter → invitee |
| workspaces | Folder | BigAutoField | workspace → Workspace, user → User |
| workspaces | AIUsage | BigAutoField | workspace + user + chat_session |
| workspaces | WorkspaceCost | BigAutoField | workspace → Workspace |
| chat | ChatSession | UUID | workspace → Workspace, user → User |
| chat | Message | UUID | session → ChatSession, sequence_number |
| chat | ChatSessionShare | UUID | chat_session + shared_with_user |
| documents | KnowledgeBase | UUID | workspace → Workspace |
| documents | Document | UUID | workspace → Workspace |
| documents | DocumentChunk | UUID | document → Document, embedding (1536d vector) |
| documents | KnowledgeBaseDocument | BigAutoField | knowledge_base + document |
| documents | ChatSessionKnowledgeBase | BigAutoField | chat_session + knowledge_base |
| documents | DocumentProcessingTask | BigAutoField | document → Document, task_id (Celery) |
| subscriptions | StripeUser | User PK | OneToOne → User |
| subscriptions | Product | product_id (str) | Stripe product ID |
| subscriptions | Price | price_id (str) | product → Product |
| subscriptions | Subscription | subscription_id (str) | stripe_user → StripeUser |
| subscriptions | SubscriptionItem | sub_item_id (str) | subscription + price |
| subscriptions | Feature | feature_id (str) | Stripe feature |
| subscriptions | ProductFeature | BigAutoField | product + feature |
Multi-Tenancy Model
HyperSaaS uses workspace-based multi-tenancy:
- Every resource (chat, document, knowledge base) belongs to a
Workspace - Users access workspaces through
WorkspaceMembership - The
WorkspaceAwareModelabstract mixin provides aworkspaceFK for any model that needs scoping - API permission classes enforce workspace membership at every endpoint
class WorkspaceAwareModel(models.Model):
workspace = models.ForeignKey(Workspace, on_delete=models.CASCADE)
class Meta:
abstract = True