0
0
Flaskframework~20 mins

Creating tables (db.create_all) in Flask - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask DB Table Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when you call db.create_all() in Flask?

Consider a Flask app using SQLAlchemy. What does db.create_all() do when executed?

Flask
from flask_sqlalchemy import SQLAlchemy
from flask import Flask

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
db = SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))

with app.app_context():
    db.create_all()
AIt drops all tables and then creates them again.
BIt deletes all existing tables and recreates them empty.
CIt only creates tables if the database is empty, otherwise does nothing.
DIt creates all tables defined by the models in the connected database if they don't exist.
Attempts:
2 left
💡 Hint

Think about what create_all() means literally and how it behaves with existing tables.

📝 Syntax
intermediate
2:00remaining
Identify the correct way to call db.create_all() in Flask

Which of the following code snippets correctly calls db.create_all() in a Flask app context?

Flask
from flask_sqlalchemy import SQLAlchemy
from flask import Flask

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)

class Product(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
Aapp.app_context().create_all(db)
Bdb.create_all(app.app_context())
C
with app.app_context():
    db.create_all()
Ddb.create_all()
Attempts:
2 left
💡 Hint

Remember that db.create_all() needs to run inside the Flask app context.

🔧 Debug
advanced
2:00remaining
Why does db.create_all() not create tables for new models?

You added a new model to your Flask app but db.create_all() does not create its table. What is the most likely cause?

Flask
class Order(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    item = db.Column(db.String(100))

# After adding this, you run:
with app.app_context():
    db.create_all()
AThe primary key is missing in the new model.
BThe new model class was not imported before calling <code>db.create_all()</code>.
CYou need to call <code>db.create_all(force=True)</code> to create new tables.
DThe database URI is incorrect, so tables cannot be created.
Attempts:
2 left
💡 Hint

Think about how SQLAlchemy knows about models when creating tables.

state_output
advanced
2:00remaining
What is the state of the database after calling db.create_all() twice?

Given a Flask app with a model User, what happens if you call db.create_all() two times in a row inside the app context?

Flask
with app.app_context():
    db.create_all()
    db.create_all()
AThe tables are created once; the second call does nothing since tables exist.
BThe tables are dropped and recreated on the second call.
CAn error is raised on the second call because tables already exist.
DThe tables are duplicated, creating multiple copies.
Attempts:
2 left
💡 Hint

Consider how create_all() behaves with existing tables.

🧠 Conceptual
expert
3:00remaining
Why should db.create_all() be called inside app.app_context()?

Explain why db.create_all() must be called inside with app.app_context(): in a Flask application.

ABecause Flask needs the app context to access configuration and database bindings during table creation.
BBecause calling it outside app context causes the database to reset.
CBecause <code>db.create_all()</code> modifies the app's routing rules which require context.
DBecause it only works inside a request context, not app context.
Attempts:
2 left
💡 Hint

Think about what the app context provides to Flask extensions like SQLAlchemy.