0
0
FastAPIframework~10 mins

Database session management in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the correct function for creating a database session.

FastAPI
from sqlalchemy.orm import [1]
Drag options to blanks, or click blank then click option'
ASessionLocal
Bsessionmaker
Ccreate_engine
DBase
Attempts:
3 left
💡 Hint
Common Mistakes
Using create_engine instead of sessionmaker
Importing Base instead of sessionmaker
Confusing SessionLocal with sessionmaker
2fill in blank
medium

Complete the code to create a new database session instance.

FastAPI
db = [1]()
Drag options to blanks, or click blank then click option'
Aengine
BBase
Csessionmaker
DSessionLocal
Attempts:
3 left
💡 Hint
Common Mistakes
Calling engine() instead of SessionLocal()
Using sessionmaker() directly without configuring it
Trying to use Base as a session
3fill in blank
hard

Fix the error in the dependency function to properly close the database session.

FastAPI
def get_db():
    db = SessionLocal()
    try:
        yield [1]
    finally:
        db.close()
Drag options to blanks, or click blank then click option'
ASessionLocal
Bengine
Cdb
DBase
Attempts:
3 left
💡 Hint
Common Mistakes
Yielding the session factory instead of the session instance
Yielding the engine or Base instead of the session
Not yielding anything
4fill in blank
hard

Fill both blanks to create a session factory with autocommit disabled and autoflush enabled.

FastAPI
SessionLocal = sessionmaker(bind=engine, autocommit=[1], autoflush=[2])
Drag options to blanks, or click blank then click option'
AFalse
BTrue
CNone
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Setting autocommit to True causing unexpected commits
Disabling autoflush leading to stale queries
Using None or 0 instead of boolean values
5fill in blank
hard

Fill all three blanks to define a FastAPI dependency that provides a database session and ensures it is closed after use.

FastAPI
def get_db():
    db = [1]()
    try:
        yield [2]
    finally:
        [3].close()
Drag options to blanks, or click blank then click option'
ASessionLocal
Bdb
Dengine
Attempts:
3 left
💡 Hint
Common Mistakes
Yielding the session factory instead of the session instance
Closing the wrong variable like engine instead of the session
Not closing the session at all