Overview
Models represent the database schema using SQLAlchemy ORM. They are infrastructure-layer concerns and should not be exposed to the domain layer.Model Architecture
Soft-Bee uses SQLAlchemy declarative models with the following characteristics:- PostgreSQL-specific types: UUID, JSON columns
- Automatic timestamps:
created_at,updated_atwith defaults - Indexing: Strategic indexes on frequently queried fields
- Type safety: Python type hints for better IDE support
User Model
Location
src/features/auth/infrastructure/models/user_model.py:7
Complete Definition
Column Definitions
Primary Key
- Uses PostgreSQL UUID type
as_uuid=Truereturns Python UUID objects instead of strings- Auto-generates UUID v4 on creation
- No need for manual ID assignment
Unique Constraints with Indexes
unique=Trueenforces uniqueness at database levelindex=Truecreates B-tree index for fast lookupsnullable=Falserequires value on insert
JSON Storage
- Stores array of refresh tokens as JSON
- PostgreSQL native JSON type for efficient querying
- Default factory returns empty list
- Supports list operations (append, remove)
Timestamps
default=datetime.utcnowsets timestamp on insertonupdate=datetime.utcnowautomatically updates on any modification- Always use UTC for consistency across timezones
Security Fields
- Password field stores hashed passwords (Argon2 or bcrypt)
- Never store plain text passwords
is_verifiedtracks email verification status
Creating Tables
With Flask-Migrate (Alembic)
Programmatically
Model Best Practices
Use Type Hints
Indexing Strategy
Index columns used in:- WHERE clauses (filters)
- JOIN conditions
- ORDER BY clauses
- UNIQUE constraints
Default Values
server_default for database-level defaults, default for application-level.
Nullable vs Required
PostgreSQL-Specific Features
UUID Type
- 128-bit identifier (more compact than string)
- Built-in PostgreSQL support
- Globally unique without coordination
JSON/JSONB
- Indexing JSON properties
- JSON operators in queries
- Faster query performance
Querying Models
Basic Queries
JSON Queries
Migrations
Column Changes
Data Migrations
Model-Entity Mapping
Models should not be exposed to the domain layer. Use mappers:Related Documentation
- Repositories - Data access patterns
- Migrations - Database schema management
- Domain Entities - Domain layer objects