Bird
0
0

Identify the error in this FastAPI code snippet using SQLAlchemy connection pooling: ```python engine = create_engine("sqlite:///./test.db", pool_size=5) SessionLocal = sessionmaker(bind=engine) @app.get("/users") def get_users(): db = SessionLocal users = db.query(User).all() db.close() return users ```

medium📝 Debug Q6 of 15
FastAPI - Database Integration
Identify the error in this FastAPI code snippet using SQLAlchemy connection pooling: ```python engine = create_engine("sqlite:///./test.db", pool_size=5) SessionLocal = sessionmaker(bind=engine) @app.get("/users") def get_users(): db = SessionLocal users = db.query(User).all() db.close() return users ```
Adb.close() should be called before querying
Bpool_size parameter is invalid for SQLite
CSessionLocal is not called to create a session instance
DThe route decorator is missing async keyword
Step-by-Step Solution
Solution:
  1. Step 1: Check how SessionLocal is used

    SessionLocal is a factory; it must be called with () to create a session instance.
  2. Step 2: Identify the error in code

    Code assigns db = SessionLocal without (), so db is the factory, not a session object.
  3. Final Answer:

    SessionLocal is not called to create a session instance -> Option C
  4. Quick Check:

    Call SessionLocal() to get session instance [OK]
Quick Trick: Call SessionLocal() to get a session, not just SessionLocal [OK]
Common Mistakes:
MISTAKES
  • Forgetting parentheses after SessionLocal
  • Thinking pool_size is invalid for SQLite
  • Closing session before query

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes