What is the best tool or ORM to manage database in Fast API? #8615
-
First Check
Commit to Help
Example Codefrom typing import List
import databases
import sqlalchemy
from fastapi import FastAPI
from pydantic import BaseModel
# SQLAlchemy specific code, as with any other app
DATABASE_URL = "sqlite:///./test.db"
# DATABASE_URL = "postgresql://user:password@postgresserver/db"
database = databases.Database(DATABASE_URL)
metadata = sqlalchemy.MetaData()
notes = sqlalchemy.Table(
"notes",
metadata,
sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),
sqlalchemy.Column("text", sqlalchemy.String),
sqlalchemy.Column("completed", sqlalchemy.Boolean),
)
engine = sqlalchemy.create_engine(
DATABASE_URL, connect_args={"check_same_thread": False}
)
metadata.create_all(engine)
class NoteIn(BaseModel):
text: str
completed: bool
class Note(BaseModel):
id: int
text: str
completed: bool
app = FastAPI()
@app.on_event("startup")
async def startup():
await database.connect()
@app.on_event("shutdown")
async def shutdown():
await database.disconnect()
@app.get("/notes/", response_model=List[Note])
async def read_notes():
query = notes.select()
return await database.fetch_all(query)
@app.post("/notes/", response_model=Note)
async def create_note(note: NoteIn):
query = notes.insert().values(text=note.text, completed=note.completed)
last_record_id = await database.execute(query)
return {**note.dict(), "id": last_record_id}DescriptionI read FastAPI documentation, but still not clear which tools should use to manage database in FastAPI projects. Operating SystemLinux, Windows Operating System DetailsNo response FastAPI Version0.75.0 Python Version3.7 + Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 13 comments 5 replies
-
|
I use SqlModel and SqlAlchemy Core. I think it better to use the official ORM tool for FastAPI :) |
Beta Was this translation helpful? Give feedback.
-
|
Yes, I think so. |
Beta Was this translation helpful? Give feedback.
-
|
eh tiangolo created |
Beta Was this translation helpful? Give feedback.
-
|
Using the async |
Beta Was this translation helpful? Give feedback.
-
|
OK, I will check more about it. My concern is tortoise-orm is growing faster than SqlAlchemy. |
Beta Was this translation helpful? Give feedback.
-
that's awesome |
Beta Was this translation helpful? Give feedback.
-
|
I made benchmark test on my PC. You can see result here |
Beta Was this translation helpful? Give feedback.
-
|
Try Prisma. Define your schema in its Prisma language, and use its generator to generate whole pydantic / db hybrid models. Then import the models module into your project. |
Beta Was this translation helpful? Give feedback.
-
|
Try SQLModel. It’s Pydantic + SQLAlchemy. Here's an example |
Beta Was this translation helpful? Give feedback.
-
|
@tiangolo would say to check SQLModel. But for the sake of answering the question... There's no "best tool or ORM to manage database in FastAPI". FastAPI works well with any tool, since it's not coupled with any. |
Beta Was this translation helpful? Give feedback.
-
|
DONT USE tortoise-orm !!! IT'S ORM BUILT ON BUGS |
Beta Was this translation helpful? Give feedback.
-
|
Piccolo ORM is an easy-to-use (both |
Beta Was this translation helpful? Give feedback.
-
|
Alembic and async sqlalchemy could be a good choice. I haven't seen tools like Tortoise ORM support less commonly used databases very well. For example, in the company I'm currently working at, we use SQLAlchemy as the core and support it with additional extensions like clickhouse sqlalchemy. When choosing a package, it will be more appropriate to also determine the database you're going to use. |
Beta Was this translation helpful? Give feedback.
@tiangolo would say to check SQLModel.
But for the sake of answering the question... There's no "best tool or ORM to manage database in FastAPI". FastAPI works well with any tool, since it's not coupled with any.