88 lines
2.3 KiB
Python
88 lines
2.3 KiB
Python
"""Base SQLModel configuration for WealthWise database models.
|
|
|
|
This module provides the base model class with common fields and utilities
|
|
for all database entities.
|
|
"""
|
|
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
from uuid import UUID, uuid4
|
|
|
|
from sqlalchemy import func
|
|
from sqlmodel import Field, SQLModel
|
|
|
|
|
|
class BaseModel(SQLModel):
|
|
"""Base model with common fields for all database tables.
|
|
|
|
All models should inherit from this class to get:
|
|
- UUID primary key
|
|
- Created and updated timestamps
|
|
- Soft delete support
|
|
|
|
Attributes:
|
|
id: Unique identifier (UUID)
|
|
created_at: Timestamp when record was created
|
|
updated_at: Timestamp when record was last updated
|
|
deleted_at: Timestamp for soft deletes (None if active)
|
|
"""
|
|
|
|
id: Optional[UUID] = Field(
|
|
default_factory=uuid4,
|
|
primary_key=True,
|
|
index=True,
|
|
description="Unique identifier",
|
|
)
|
|
|
|
created_at: Optional[datetime] = Field(
|
|
default=None,
|
|
sa_column_kwargs={
|
|
"server_default": func.now(),
|
|
"nullable": False,
|
|
},
|
|
description="Timestamp when record was created",
|
|
)
|
|
|
|
updated_at: Optional[datetime] = Field(
|
|
default=None,
|
|
sa_column_kwargs={
|
|
"server_default": func.now(),
|
|
"onupdate": func.now(),
|
|
"nullable": False,
|
|
},
|
|
description="Timestamp when record was last updated",
|
|
)
|
|
|
|
deleted_at: Optional[datetime] = Field(
|
|
default=None,
|
|
description="Timestamp for soft delete (None if record is active)",
|
|
)
|
|
|
|
class Config:
|
|
"""Pydantic configuration."""
|
|
# Allow arbitrary types for UUID handling
|
|
arbitrary_types_allowed = True
|
|
|
|
|
|
class TimestampMixin(SQLModel):
|
|
"""Mixin for models that only need timestamp fields (no ID).
|
|
|
|
Use this for association tables or when ID is handled differently.
|
|
"""
|
|
|
|
created_at: Optional[datetime] = Field(
|
|
default=None,
|
|
sa_column_kwargs={
|
|
"server_default": func.now(),
|
|
"nullable": False,
|
|
},
|
|
)
|
|
|
|
updated_at: Optional[datetime] = Field(
|
|
default=None,
|
|
sa_column_kwargs={
|
|
"server_default": func.now(),
|
|
"onupdate": func.now(),
|
|
"nullable": False,
|
|
},
|
|
) |