0
0
FastAPIframework~10 mins

SQLAlchemy setup with 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 SQLAlchemy base class.

FastAPI
from sqlalchemy.ext.declarative import [1]
Drag options to blanks, or click blank then click option'
Adeclarative_base
BSessionLocal
Ccreate_engine
DBase
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Base' directly without importing declarative_base
Confusing create_engine with declarative_base
2fill in blank
medium

Complete the code to create the SQLAlchemy engine with a SQLite database URL.

FastAPI
engine = create_engine('[1]', connect_args={"check_same_thread": False})
Drag options to blanks, or click blank then click option'
A"sqlite:///./test.db"
B"postgresql://user:pass@localhost/dbname"
C"mysql://user:pass@localhost/dbname"
D"oracle://user:pass@localhost/dbname"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a PostgreSQL or MySQL URL instead of SQLite
Missing the triple slashes after 'sqlite:'
3fill in blank
hard

Fix the error in the session creation code by filling the blank.

FastAPI
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=[1])
Drag options to blanks, or click blank then click option'
ABase
Bengine
CSessionLocal
Dcreate_engine
Attempts:
3 left
💡 Hint
Common Mistakes
Binding to Base instead of engine
Binding to the sessionmaker itself
4fill in blank
hard

Fill both blanks to define a FastAPI dependency that provides a database session and closes it after use.

FastAPI
def get_db():
    db = [1]()
    try:
        yield db
    finally:
        db.[2]()
Drag options to blanks, or click blank then click option'
ASessionLocal
Bengine
Cclose
Dcommit
Attempts:
3 left
💡 Hint
Common Mistakes
Using engine instead of SessionLocal to create session
Calling commit instead of close in finally block
5fill in blank
hard

Fill all three blanks to define a SQLAlchemy model class with a table name, id column, and a string column.

FastAPI
class User([1]):
    __tablename__ = '[2]'
    id = Column(Integer, primary_key=True, index=True)
    name = Column([3])
Drag options to blanks, or click blank then click option'
ABase
Busers
CString
DInteger
Attempts:
3 left
💡 Hint
Common Mistakes
Using Integer for the name column
Forgetting to inherit from Base
Using singular table name