Given this Flask app and model setup, what will User.query.count() return after adding two users and committing?
from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), nullable=False) with app.app_context(): db.create_all() user1 = User(name='Alice') user2 = User(name='Bob') db.session.add(user1) db.session.add(user2) db.session.commit() count = User.query.count()
Remember that db.create_all() and queries require the app context.
After adding two users and committing, User.query.count() returns 2 because both users are saved in the database.
Choose the correct way to set up Flask-SQLAlchemy when using an app factory pattern.
from flask import Flask from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() def create_app(): app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db' # Initialize db here return app
When using app factory, you create the db object outside and initialize it inside.
In app factory pattern, db = SQLAlchemy() is outside, and db.init_app(app) is called inside create_app() before returning the app.
What error does this code raise and why?
from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) class Product(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80), nullable=False) product = Product(name='Book') db.session.add(product) db.session.commit()
Check if the code runs inside the Flask app context.
The code tries to add and commit outside the Flask app context, causing a RuntimeError.
user.email after this code runs?Consider this Flask-SQLAlchemy model and code snippet. What will user.email be?
from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(50), nullable=False) email = db.Column(db.String(120), unique=True, nullable=True) with app.app_context(): db.create_all() user = User(username='john') db.session.add(user) db.session.commit() user = User.query.first()
Check the default value of email when not set.
The email column is nullable and not set, so its value is None.
Choose the most accurate description of how Flask-SQLAlchemy manages database sessions.
Think about how Flask handles multiple requests and how SQLAlchemy sessions should behave.
Flask-SQLAlchemy uses a scoped session that is tied to the Flask app context, ensuring thread safety and proper session lifecycle per request.