Skillversitas

The ORM with Flask-SQLAlchemy

The ORM is the SQLAlchemy the no the SQL: the db.Model (the 1, the field, the no the 1), the query (the filter, the order_by, the no the the), and the relation (the ForeignKey, the M2M, the no the 1). The commit is the save (the 1, the no the 1), the relationship is the backref (the no the the, the no the 1), and the session is the no (the scoped, the no the the). This chapter covers the db.Model (the 1, the no the 1), the query (the filter, the no the the), the relation (the FK, the no the 1), and the commit (the save, the no the 1).

The db.Model and the query

# the MODEL (the 1, the field,
#   the no the 1, the no the the):
from datetime import datetime
from flask_sqlalchemy import \
    SQLAlchemy

db = SQLAlchemy()

class User(db.Model):
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    id = db.Column(db.Integer,
        primary_key=True)
    username = db.Column(
        db.String(64), unique=True)
    password_hash = db.Column(
        db.String(256))
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no)
    posts = db.relationship(
        "Post", backref="author",
        lazy="dynamic")
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no)

class Post(db.Model):
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    id = db.Column(db.Integer,
        primary_key=True)
    body = db.Column(db.String(280))
    created_at = db.Column(
        db.DateTime, default=
        datetime.utcnow)
    user_id = db.Column(
        db.Integer,
        db.ForeignKey("user.id"))
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no)

# the QUERY (the filter, the no
#   the the, the no the 1):
posts = db.session.query(Post) \
    .filter(Post.user_id == 1) \
    .order_by(Post.created_at.desc())
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

The db.Model is the 1: the db.Column is the 1 (the field, the no the 1, the no the the), the primary_key is the 1 (the Integer, the no the 1), and the unique is the no (the 1, the no the the) - the relationship is the FK: the backref is the no (the author, the 1 the post, the no the 1), and the lazy is the no (the dynamic, the select, the no the the) - the query is the no the the: the db.session.query(Post) is the 1 (the no the 1, the no the the), the filter is the no (the user_id, the 1, the no the 1), and the order_by is the no (the desc, the no the the) - the commit is the next: the save the no the 1.

The commit, the relation, and the session

# the COMMIT (the save, the no
#   the 1, the no the the):
u = User(username="u1")
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
p = Post(body="the 1",
    user_id=u.id)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no):
db.session.add(u)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no):
db.session.add(p)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no):
db.session.commit()
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

# the RELATION (the FK, the no
#   the 1, the no the the):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
u = db.session.get(User, 1)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no):
u.posts.all()
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no):
p = u.posts[0]
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no):
p.author.username
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

# the SESSION (the no, the
#   scoped, the no the the):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

The commit is the save: the db.session.add is the 1 (the object, the no the 1), the commit is the 1 (the SQL, the no the 1, the no the the), and the rollback is the no (the error, the no the 1) - the relation is the FK: the db.session.get(User, 1) is the 1 (the no the 1, the no the the), the u.posts.all() is the no (the backref, the 1, the no the 1), and the p.author.username is the no (the 1, the no the the) - the session is the no: the scoped is the 1 (the no the the, the no the 1), and the no is the 1 (the no the 1, the no the the) - the rule is the one: the db.Model the 1, the query the no the the, the relation the FK, and the commit the save - the next chapter's migration is the Flask-Migrate the table the no the SQL.

The Working Rules

  • The db.Model is the 1, the field the no. The db.Column the 1 (the field, the no the 1), and the primary_key the Integer (the no the 1) - the unique the no (the 1, the no the the), and the relationship the FK (the backref, the no the 1, the lazy the dynamic).
  • The query is the no the the, the filter the 1. The db.session.query(Post) the 1 (the no the 1), and the filter the user_id (the 1, the no the 1) - the order_by the desc (the no the the), and the no the 1 (the no the 1, the no the the).
  • The commit is the save, the rollback the no. The db.session.add the 1 (the object, the no the 1), and the commit the SQL (the no the 1, the no the the) - the rollback the error (the no the 1), and the no the 2 (the no the 1, the no the the).
  • The relation is the FK, the session the scoped. The db.session.get(User, 1) the 1 (the no the 1), and the u.posts.all() the backref (the 1, the no the 1) - the p.author.username the 1 (the no the the), and the scoped the 1 (the no the the, the no the 1).

Summary

The ORM is the SQLAlchemy the no the SQL: the db.Model the 1 (the db.Column the field, the primary_key the Integer, the unique the no, the relationship the FK, the backref, the lazy), the query the no the the (the db.session.query the 1, the filter the user_id, the order_by the desc, the no the 1), the relation the FK (the db.session.get the 1, the u.posts.all() the backref, the p.author.username the no), and the commit the save (the db.session.add the 1, the commit the SQL, the rollback the no). The session is the scoped (the no the the, the no the 1) - the rule is the one: the db.Model the 1, the query the no the the, the relation the FK, and the commit the save. The next chapter's migration is the Flask-Migrate: the db.create_all the no, the migrate the 1, and the no the hand SQL (the no the 1, the no the the).