W przeciwieństwie do Django, FastAPI jest niefrasobliwe w kwestii baz danych — ty wybierasz bibliotekę. Popularnym wyborem jest SQLAlchemy (często z SQLModel, autorstwa twórcy FastAPI, która łączy SQLAlchemy + Pydantic). Sesje bazy danych są dostarczane poprzez dependency injection.
Konfiguracja SQLAlchemy
sqlalchemy create_engine
sqlalchemy.orm sessionmaker, declarative_base
engine = create_engine()
SessionLocal = sessionmaker(bind=engine)
Base = declarative_base()
():
__tablename__ =
= Column(Integer, primary_key=)
name = Column(String)
email = Column(String, unique=)
