Junior — Middle
Which ORM frameworks do you use when developing projects with FastAPI?
sobes.tech AI
Answer from AI
When developing projects with FastAPI, I often use the following ORM frameworks:
-
SQLAlchemy — the most popular and powerful ORM for Python, well integrated with FastAPI. It allows working with various databases and supports declarative model descriptions.
-
Tortoise ORM — an asynchronous ORM suitable for asynchronous applications on FastAPI. It provides a convenient syntax and migration support through separate tools.
Example of using SQLAlchemy with FastAPI:
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True)
The choice of ORM depends on the project requirements, the need for asynchrony, and personal preferences regarding database interaction style.