models/base.py
¶
Item | Value |
---|---|
Layer | Models |
Responsibility | Provides the SQLAlchemy declarative base class with common columns and methods |
Status | 🟢 Done |
1. Purpose¶
This file establishes the foundation for all database models in the application. It defines a common base class that all entity models inherit from, providing consistent ID, timestamp tracking, and serialization capabilities.
2. Public API¶
Symbol | Type | Description |
---|---|---|
Base | DeclarativeBase | SQLAlchemy base class for all models |
Common Columns on All Models:
id
: Integer primary key, auto-incrementingcreated_at
: DateTime with automatic creation timestampupdated_at
: DateTime that updates on every record change
Common Methods:
to_dict()
: Converts model instance to a dictionary__repr__()
: String representation for debugging
3. Behaviour & Edge-Cases¶
- All models automatically get
id
,created_at
, andupdated_at
columns created_at
andupdated_at
are set using SQLAlchemy'sfunc.now()
function:created_at
is set once at record creationupdated_at
is set at creation and updated with every change- The
to_dict()
method performs a shallow conversion of model attributes to a dictionary
4. Dependencies¶
-
Internal: None
-
External:
sqlalchemy.orm
: For ORM functionalitysqlalchemy
: For database column types and functions
5. Tests¶
Test file | Scenario |
---|---|
backend/tests/models/test_base.py | Tests for Base.to_dict() and Base.repr() methods |
6. Open TODOs¶
- Consider adding a
to_json()
method for API responses - Add support for relationship serialization in
to_dict()
- Add option to exclude certain fields in serialization