-
Notifications
You must be signed in to change notification settings - Fork 0
Description
To ensure our API docs are accurate, we need to create a database table class, which inherits from SQLAlchemy's Base and a response model, which inherits from Pydantic's BaseModel. This results in a lot of code duplication and having to create custom serialization functions whenever we have an object that has a field that's more than just a string, number or float (like a datetime).
I use the API docs to automatically generate the types and services we use on the sites, so I have a lot of interest in rewriting the APIs using a library called SQLModel. It's a wrapper around SQLAlchemy and Pydantic. Instead of creating both a TableModel(Base) and a ResponseModel(BaseModel), you create a single model called Model(SQLModel, table=true/false). This allows us to declare a single model for both the database return models and the FastAPI response models. It would also save us from having to write custom validation and serialization, by allowing us to leverage Pydantic's function https://docs.pydantic.dev/latest/concepts/serialization/#modelmodel_dump.
I actually did rewrite the /auth endpoint using SQLModel, but I ended up dropping it because it would force us to rewrite all of our endpoints using the SQLModel types instead of SQLAlchemy types. It also has a strange way of doing UPDATE, so we'd probably have to wait until fastapi/sqlmodel#1342 is merged before I'd want to fully commit into rewriting the CRUD operations.