ต่างจาก Django, FastAPI เป็นแบบ ไม่มีความเห็นบังคับ (unopinionated) เกี่ยวกับฐานข้อมูล — คุณเลือก library เอง ตัวเลือกที่ใช้บ่อยคือ SQLAlchemy (มักใช้ร่วมกับ SQLModel ซึ่งสร้างโดยผู้สร้าง FastAPI โดยรวม SQLAlchemy + Pydantic เข้าด้วยกัน) database sessions ถูกจัดหาผ่าน dependency injection
การตั้งค่า 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=)
