Complete the code to import the SQLAlchemy base class.
from sqlalchemy.ext.declarative import [1]
The declarative_base function creates a base class for model classes in SQLAlchemy.
Complete the code to create the SQLAlchemy engine with a SQLite database URL.
engine = create_engine('[1]', connect_args={"check_same_thread": False})
The SQLite database URL format is sqlite:///./filename.db. This example uses test.db in the current folder.
Fix the error in the session creation code by filling the blank.
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=[1])
The bind parameter must be the SQLAlchemy engine instance to connect sessions to the database.
Fill both blanks to define a FastAPI dependency that provides a database session and closes it after use.
def get_db(): db = [1]() try: yield db finally: db.[2]()
The function creates a session from SessionLocal and closes it with close() after use.
Fill all three blanks to define a SQLAlchemy model class with a table name, id column, and a string column.
class User([1]): __tablename__ = '[2]' id = Column(Integer, primary_key=True, index=True) name = Column([3])
The model class inherits from Base. The table name is users. The name column uses the String type.