← Flask tutorial

Flask

Flask-SQLAlchemy Models

Flask doesn't include an ORM out of the box, so most real projects add Flask-SQLAlchemy — a deliberate choice, not a missing feature, in keeping with Flask's "add only what you need" philosophy.

Example: a model and a query

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy(app)

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200))

# Usage:
posts = Post.query.all()

Post.query.all() returns a Python list of every Post row — familiar-looking if you've seen Django's Post.objects.all() or Laravel's Post::all(), since all three frameworks solve the same underlying problem (querying a database without writing raw SQL) with genuinely similar-shaped APIs.

Example
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy(app)

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200))

# Usage:
posts = Post.query.all()
Output
posts becomes a Python list of every Post row in the database.