Adds SQLAlchemy support to Flask
天问 2cd2e11d29 Update 'README.md' | 1 year ago | |
---|---|---|
README.md | 1 year ago |
Adds SQLAlchemy support to Flask
pip install -U Flask-SQLAlchemy
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///example.sqlite"
class Base(DeclarativeBase):
pass
db = SQLAlchemy(app, model_class=Base)
class User(db.Model):
id: Mapped[int] = mapped_column(db.Integer, primary_key=True)
username: Mapped[str] = mapped_column(db.String, unique=True, nullable=False)
with app.app_context():
db.create_all()
db.session.add(User(username="example"))
db.session.commit()
users = db.session.execute(db.select(User)).scalars()