0
0
FastAPIframework~20 mins

Why databases persist data in FastAPI - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Database Persistence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do databases persist data?

Which of the following best explains why databases persist data?

ATo delete old data automatically after the application closes
BTo keep data available even after the application or server stops running
CTo speed up the application by storing data only in memory temporarily
DTo prevent users from accessing data during server downtime
Attempts:
2 left
💡 Hint

Think about what happens to data when the computer or app is turned off.

component_behavior
intermediate
2:00remaining
What happens to data stored in a database after server restart?

Consider a FastAPI app connected to a database. What happens to the data stored in the database if the server restarts?

AThe data becomes inaccessible until manually restored
BThe data is erased and lost after the restart
CThe data is moved to a temporary cache and then deleted
DThe data remains intact and accessible after the restart
Attempts:
2 left
💡 Hint

Think about the purpose of persistent storage.

📝 Syntax
advanced
2:30remaining
Identify the correct FastAPI code snippet to save data persistently

Which FastAPI code snippet correctly saves data to a database so it persists?

FastAPI
from fastapi import FastAPI
from sqlalchemy.orm import Session
from models import Item

app = FastAPI()

@app.post('/items/')
async def create_item(item: Item, db: Session):
    # Save item to database
    ...
A
db.insert(item)
db.save()
return item
B
db.save(item)
return item
C
db.add(item)
db.commit()
return item
D
db.push(item)
db.commit()
return item
Attempts:
2 left
💡 Hint

Look for the standard SQLAlchemy methods to add and save data.

state_output
advanced
2:00remaining
What is the output after restarting FastAPI app with saved data?

Given a FastAPI app that saves user data to a database, what will be the output of fetching users after restarting the app?

FastAPI
from fastapi import FastAPI
from sqlalchemy.orm import Session
from models import User

app = FastAPI()

@app.get('/users/')
async def read_users(db: Session):
    return db.query(User).all()
A[{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}]
B[]
CNone
DError: database connection lost
Attempts:
2 left
💡 Hint

Think about whether data saved before restart is still there.

🔧 Debug
expert
2:30remaining
Why does this FastAPI app lose data after restart?

Review this FastAPI code snippet. Why does the data disappear after restarting the app?

FastAPI
from fastapi import FastAPI

app = FastAPI()

items = []

@app.post('/items/')
async def add_item(name: str):
    items.append(name)
    return {'items': items}
ABecause data is stored only in a list in memory, not in a database
BBecause the list is cleared automatically on each request
CBecause FastAPI deletes variables after each function call
DBecause the app uses an incorrect HTTP method
Attempts:
2 left
💡 Hint

Think about where the data is saved and what happens when the app restarts.