Complete the code to import the correct function for creating a database session.
from sqlalchemy.orm import [1]
The sessionmaker function is used to create a new session factory in SQLAlchemy, which is essential for managing database sessions in FastAPI.
Complete the code to create a new database session instance.
db = [1]()SessionLocal is the session factory created by sessionmaker. Calling it returns a new session instance.
Fix the error in the dependency function to properly close the database session.
def get_db(): db = SessionLocal() try: yield [1] finally: db.close()
The function should yield the session instance db so that the caller can use it. Yielding SessionLocal or other objects is incorrect.
Fill both blanks to create a session factory with autocommit disabled and autoflush enabled.
SessionLocal = sessionmaker(bind=engine, autocommit=[1], autoflush=[2])
By default, autocommit should be False to control transactions manually, and autoflush is often set to True to flush changes automatically before queries.
Fill all three blanks to define a FastAPI dependency that provides a database session and ensures it is closed after use.
def get_db(): db = [1]() try: yield [2] finally: [3].close()
The dependency function creates a session by calling SessionLocal(), yields the session db for use, and finally closes the session with db.close().