← 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.
Ready to go further?

These free tutorials are a preview, our Backend Development cohort covers it in depth, with a real project and a cohort alongside you. Active

Enroll in Backend Development
Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account