Bird
0
0

Identify the error in this FastAPI SQLAlchemy session usage:

medium📝 Debug Q6 of 15
FastAPI - Database Integration
Identify the error in this FastAPI SQLAlchemy session usage:
def get_user(db: SessionLocal):
    user = db.query(User).first()
    return user

@app.get('/user')
def read_user(db: SessionLocal = Depends()):
    return get_user(db)
AFunction get_user should not return user.
BDepends() missing the session dependency function.
Cdb.query() should be db.session.query().
DUser model is not imported.
Step-by-Step Solution
Solution:
  1. Step 1: Check Depends() usage

    Depends() requires a callable that provides the session, usually a function that yields a session instance.
  2. Step 2: Identify missing dependency function

    Here, Depends() is empty, so FastAPI cannot inject the session properly, causing an error.
  3. Final Answer:

    Depends() missing the session dependency function. -> Option B
  4. Quick Check:

    Depends() needs a provider function [OK]
Quick Trick: Always pass session provider to Depends() [OK]
Common Mistakes:
MISTAKES
  • Leaving Depends() empty
  • Confusing query syntax
  • Ignoring import errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes