Bird
Raised Fist0
Flaskframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the db.create_all() function do in a Flask application?
easy
A. It updates existing tables to match model changes automatically.
B. It deletes all tables from the database.
C. It creates all tables defined by your models in the database if they don't exist.
D. It runs all database queries defined in your app.

Solution

  1. Step 1: Understand the purpose of db.create_all()

    This function creates tables in the database based on your model definitions if those tables do not already exist.
  2. Step 2: Recognize what it does not do

    It does not delete tables, update existing tables, or run queries; it only creates missing tables.
  3. Final Answer:

    It creates all tables defined by your models in the database if they don't exist. -> Option C
  4. Quick Check:

    db.create_all() creates missing tables [OK]
Hint: Remember: create_all only adds missing tables, no updates [OK]
Common Mistakes:
  • Thinking it updates existing tables automatically
  • Confusing it with deleting tables
  • Assuming it runs queries or migrations
2. Which of the following is the correct way to use db.create_all() in a Flask app?
easy
A. Call db.create_all() inside the Flask app context.
B. Call db.create_all() after closing the app.
C. Call db.create_all() before defining your models.
D. Call db.create_all() outside the Flask app context.

Solution

  1. Step 1: Understand Flask app context requirement

    Database operations like db.create_all() must run inside the Flask app context to access configuration and database connections properly.
  2. Step 2: Identify correct usage

    Calling db.create_all() inside the app context ensures tables are created without errors.
  3. Final Answer:

    Call db.create_all() inside the Flask app context. -> Option A
  4. Quick Check:

    db.create_all() requires app context [OK]
Hint: Always run db.create_all() inside app context [OK]
Common Mistakes:
  • Calling create_all outside app context causing errors
  • Calling create_all before models are defined
  • Calling create_all after app is closed
3. Given this Flask code snippet, what will happen when db.create_all() is called?
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
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()
medium
A. An error will occur because the app context is missing.
B. A new table named 'user' will be created in the database if it doesn't exist.
C. The existing 'user' table will be dropped and recreated.
D. Nothing happens because create_all does not create tables.

Solution

  1. Step 1: Check app context usage

    The code calls db.create_all() inside app.app_context(), so the context is correct.
  2. Step 2: Understand create_all behavior

    It creates missing tables based on models. Since 'User' model exists, a 'user' table will be created if not present.
  3. Final Answer:

    A new table named 'user' will be created in the database if it doesn't exist. -> Option B
  4. Quick Check:

    create_all creates missing tables inside app context [OK]
Hint: create_all adds missing tables, does not drop existing [OK]
Common Mistakes:
  • Thinking create_all drops tables
  • Forgetting app context causes errors
  • Assuming create_all does nothing
4. You run db.create_all() but your new model's table is not created. What is the most likely cause?
medium
A. All of the above.
B. You defined the model after calling db.create_all().
C. The database URI is incorrect or missing.
D. You forgot to run db.create_all() inside the Flask app context.

Solution

  1. Step 1: Check app context usage

    Running db.create_all() outside app context causes tables not to be created.
  2. Step 2: Check model definition timing and database URI

    If models are defined after calling db.create_all(), their tables won't be created. Also, an incorrect or missing database URI prevents connection.
  3. Final Answer:

    All of the above. -> Option A
  4. Quick Check:

    App context, model timing, and URI all affect create_all success [OK]
Hint: Check app context, model order, and DB URI if tables missing [OK]
Common Mistakes:
  • Ignoring app context errors
  • Calling create_all before defining models
  • Not setting database URI correctly
5. You want to add a new column to an existing table after calling db.create_all(). What will happen if you call db.create_all() again without migrations?
hard
A. An error will occur because the table schema changed.
B. The new column will be added automatically to the existing table.
C. The existing table will be dropped and recreated with the new column.
D. Nothing changes; db.create_all() does not modify existing tables.

Solution

  1. Step 1: Understand create_all limitations

    db.create_all() only creates missing tables; it does not alter existing tables or add columns.
  2. Step 2: Recognize need for migrations

    To change existing tables (like adding columns), you must use migration tools such as Flask-Migrate.
  3. Final Answer:

    Nothing changes; db.create_all() does not modify existing tables. -> Option D
  4. Quick Check:

    create_all creates tables but does not alter existing ones [OK]
Hint: Use migrations to change tables; create_all only adds missing tables [OK]
Common Mistakes:
  • Expecting create_all to update tables
  • Not using migration tools for schema changes
  • Assuming create_all drops and recreates tables
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard