Bird
0
0

What is wrong with this FastAPI startup event code using databases library?

medium📝 Debug Q7 of 15
FastAPI - Database Integration
What is wrong with this FastAPI startup event code using databases library?
@app.on_event("startup")
async def startup():
    database.connect()
Adatabase.connect() is not a valid method
BStartup event should be synchronous
CMissing await before database.connect()
Ddatabase variable is not defined
Step-by-Step Solution
Solution:
  1. Step 1: Check async method call

    The connect() method is asynchronous and requires await when called inside an async function.
  2. Step 2: Identify missing await

    The code calls database.connect() without await, which will cause a coroutine to be created but not executed.
  3. Final Answer:

    Missing await before database.connect() -> Option C
  4. Quick Check:

    Async connect needs await = B [OK]
Quick Trick: Await async connect in startup event [OK]
Common Mistakes:
MISTAKES
  • Calling async connect without await
  • Thinking startup events must be sync
  • Forgetting to define database variable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes