What is SQLModel and how does it relate to FastAPI, Pydantic, and SQLAlchemy?
SQLModel is a package created by the author of FastAPI that combines SQLAlchemy and Pydantic. It pairs SQLAlchemy's ORM with Pydantic's data definition and validation, and it repurposes certain FastAPI web development techniques for relational databases. In relation to FastAPI, SQLModel is designed to work naturally with it because it comes from the same author and combines FastAPI-style tools with database models.
According to the book, SQLModel was made by the author of FastAPI by combining aspects of FastAPI, Pydantic, and SQLAlchemy. It is described as a combination of SQLAlchemy and Pydantic. SQLModel matches SQLAlchemy's ORM with Pydantic's data definition and validation, meaning database models can also serve as Pydantic models for request and response handling. The book also says SQLModel repurposes development techniques from the web world to relational databases, which ties it closely to FastAPI's ecosystem and its use of Pydantic for data validation.
Key points
- SQLModel is by the same author as FastAPI.
- It combines aspects of FastAPI, Pydantic, and SQLAlchemy.
- It matches SQLAlchemy's ORM with Pydantic's data definition and validation.
- It can be viewed as a combination of SQLAlchemy and Pydantic.
- It applies FastAPI-style web development techniques to relational databases.
Related questions
FastAPI: Modern Python Web Development
Bill Lubanovic;
First Edition · O'Reilly Media, Inc.