SQL (Relational) Databases - FastAPI
The new docs will include Pydantic v2 and will use SQLModel (which is also based on SQLAlchemy) once it is updated to use Pydantic v2 as well. FastAPI doesn't require you to use a SQL (relational) database. But you can use any relational database that you want. Here we'll see an example using SQLAlchemy. You can easily adapt it to any database supported by SQLAlchemy, like: In this example, we'll use SQLite, because it uses a single file and Python has integrated support. So, you can copy this example and run it as is. Later, for your production application, you might want to use a database server like PostgreSQL. Tip There is an official project generator with FastAPI and PostgreSQL, all based on Docker, including a frontend and more tools: https://github.com/tiangolo/full-stack-fastapi-postgresql Note Notice that most of the code is the standard SQLAlchemy code you would use with any framework. The FastAPI specific code is as small as always. FastAPI works with any database and any s
SQL (Relational) Databases ¶ FastAPI doesn't require you to use a SQL (relational) database. But you can use any database that you want. Here we'll see an example using SQLModel . SQLModel is built on top of SQLAlchemy and Pydantic. It was made by the same author of FastAPI to be the perfect match for FastAPI applications that need to use SQL databases . Tip You could use any other SQL or NoSQL database library you want (in some cases called "ORMs" ), FastAPI doesn't force you to use anything. 😎 As SQLModel is based on SQLAlchemy, you can easily use any database supported by SQLAlchemy (
Explore this link on the map →related reading
- The Flask Mega-Tutorial, Part IV: Database - miguelgrinberg.comblog.miguelgrinberg.com
- Here is the reason why SQLAlchemy is so popular. | Towards Data Sciencetowardsdatascience.com
- Welcome to Pydantic | Pydantic Docsdocs.pydantic.dev
- SQLAlchemy in Flask | G-Testerhuohuoren4.github.io
- Cookbookcookbook.openai.com
- Python: Just write SQLjoaodlf.com
- SQLAlchemy - Quick Guidetutorialspoint.com
- Bigger Applications - Multiple Files - FastAPIfastapi.tiangolo.com
- Building data-centric apps with a reactive relational databaseriffle.systems
- Against SQLscattered-thoughts.net
- Object Relational Tutorial - SQLAlchemy 1.3 Documentationdocs.sqlalchemy.org
- SQL as APIvalentin.willscher.de