0
0
Flaskframework~5 mins

Creating tables (db.create_all) in Flask - Quick Revision & Summary

Choose your learning style9 modes available
Recall & Review
beginner
What does db.create_all() do in Flask-SQLAlchemy?

db.create_all() creates all the tables defined by your models in the connected database. It sets up the database structure based on your Python classes.

Click to reveal answer
beginner
When should you call db.create_all() in a Flask app?

You call db.create_all() after defining your models and before running your app, usually once to set up the database tables.

Click to reveal answer
intermediate
Does db.create_all() update existing tables if the model changes?

No, db.create_all() only creates tables if they don't exist. It does not modify existing tables. For updates, you need migrations.

Click to reveal answer
beginner
What is a simple example of using db.create_all() in a Flask app?
<pre>from flask import Flask
from flask_sqlalchemy import SQLAlchemy

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

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

with app.app_context():
    db.create_all()</pre>
Click to reveal answer
intermediate
Why do you need app.app_context() when calling db.create_all()?

Flask needs an application context to know which app and database to use. app.app_context() activates this context so db.create_all() works properly.

Click to reveal answer
What does db.create_all() do in Flask-SQLAlchemy?
ACreates all tables defined by models if they don't exist
BDeletes all tables in the database
CUpdates existing tables to match models
DRuns database migrations automatically
Which of these is required before calling db.create_all()?
AA migration script
BA running web server
CA database backup
DAn active Flask application context
If you change a model after calling db.create_all(), what happens when you call it again?
ATables are updated automatically
BNo changes happen to existing tables
COld tables are deleted and recreated
DAn error is thrown
Which database URI is used in this example: sqlite:///mydb.sqlite?
AA local SQLite file database
BA remote MySQL server
CAn in-memory PostgreSQL database
DA cloud MongoDB database
What is the main limitation of db.create_all()?
AIt requires manual SQL commands
BIt only works with MySQL
CIt cannot modify existing tables
DIt deletes all data when run
Explain how db.create_all() works and when you should use it in a Flask app.
Think about setting up your database structure before running your app.
You got /4 concepts.
    Describe why db.create_all() is not enough to update your database schema after model changes.
    Consider what happens when your app grows and models change.
    You got /4 concepts.