Overview
Flask-SQLAlchemy 3.1, released on September 12, 2023, fixes bugs and improves SQLAlchemy 2.0 compatibility.
Main Features
SQLAlchemy 2.0 compatibility
Integration with the SQLAlchemy 2.0 query style is complete, using select() instead of query.
python
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import select
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
# SQLAlchemy 2.0 style
users = db.session.execute(select(User)).scalars().all()
Bug fixes
Several fixes improve session management and connection cleanup at the end of requests.
python
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
db = SQLAlchemy(app)
# Session is automatically cleaned up
# at end of request (improved in 3.1)
