-
Notifications
You must be signed in to change notification settings - Fork 0
Description
Yes, it's definitely possible to use inheritance to manage both the SQLModel definition and the FastAPI response model from a single source of truth. This approach can help reduce code duplication and make it easier to maintain consistency between your database model and API response.
Here's how you can refactor your code to achieve this:
from sqlmodel import SQLModel, Field, Relationship
from pydantic import BaseModel
from typing import Optional, List
from datetime import datetime
from sqlalchemy import Column
class GridBase(SQLModel):
status: GridStatus = Field(default=GridStatus.NONE)
name: str
scan_start_time: Optional[datetime] = Field(default=None)
scan_end_time: Optional[datetime] = Field(default=None)
class Grid(GridBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
session_id: Optional[int] = Field(default=None, foreign_key="session.id")
status: GridStatus = Field(default=GridStatus.NONE, sa_column=Column(GridStatusType()))
session: Optional[Session] = Relationship(back_populates="grids")
gridsquares: List["GridSquare"] = Relationship(back_populates="grid", cascade_delete=True)
class GridResponse(GridBase):
id: int
session_id: Optional[int]
class Config:
from_attributes = TrueIn this refactored version:
-
We create a
GridBaseclass that contains the common fields between the SQLModel and the response model. -
The
Gridclass inherits fromGridBaseand adds the database-specific fields and relationships. -
The
GridResponseclass also inherits fromGridBaseand adds theidandsession_idfields that are specific to the response.
This approach allows you to maintain a single source of truth for the common fields in GridBase, while still having the flexibility to add database-specific or response-specific fields in the respective classes.
Note that we keep the sa_column=Column(GridStatusType()) in the Grid class because it's specific to the SQLAlchemy configuration and not needed in the response model.
This structure makes it easier to keep your database model and API response in sync, as any changes to the common fields can be made in the GridBase class and will be reflected in both the Grid and GridResponse classes.