SQLAlchemy

时间:2017-12-14 15:37:09

标签: python postgresql sqlalchemy flask-sqlalchemy

我有以下查询:

SELECT b.name, c.* FROM branches b, 
     LATERAL (SELECT * FROM commits WHERE b.id = commits.branch_id  ORDER BY authored_date desc LIMIT 4) c

哪个适用于我,但我不知道我可以使用与SQLAlchemy相同的查询,因为以后没有定义任何内容。我正在使用Flask-SQLAlchemy。 这是我的模特:

class Commits(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    branch_id = db.Column(db.Integer,db.ForeignKey("branches.id"), nullable=False)
    created_date = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    commit_id = db.Column(db.String, nullable=False)
    commit_short_id = db.Column(db.String, nullable=False)
    commit_message = db.Column(db.Text, nullable=False)
    author_name = db.Column(db.String, nullable=False)
    author_email = db.Column(db.String, nullable=True, default="")
    authored_date = db.Column(db.DateTime, nullable=False)
    committer_name = db.Column(db.String, nullable=False)
    commiter_email = db.Column(db.String, nullable=True, default="")
    commited_date = db.Column(db.DateTime, nullable=True)

class Branches(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    created_date = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    name = db.Column(db.String, nullable=True)
    default = db.Column(db.Boolean, default=False, nullable=False)
    merged = db.Column(db.Boolean, default=False, nullable=False)

    commits = db.relationship("Commits", lazy="joined", backref=db.backref("branches", lazy="joined"))

1 个答案:

答案 0 :(得分:1)

SQLAlchemy执行support LATERAL

In [19]: commits = db.session.query(Commits).\
    ...:     filter(Commits.branch_id == Branches.id).\
    ...:     order_by(Commits.authored_date.desc()).\
    ...:     limit(4).\
    ...:     subquery().\
    ...:     lateral()

In [20]: commits = db.aliased(Commits, commits)

In [21]: db.session.query(Branches.name, commits).\
    ...:     options(db.lazyload("branches"))
Out[21]: <sqlalchemy.orm.query.Query at 0x7f5c53ec2e10>

In [22]: print(_)
SELECT branches.name AS branches_name, anon_1.id AS anon_1_id, anon_1.branch_id AS anon_1_branch_id, anon_1.created_date AS anon_1_created_date, anon_1.commit_id AS anon_1_commit_id, anon_1.commit_short_id AS anon_1_commit_short_id, anon_1.commit_message AS anon_1_commit_message, anon_1.author_name AS anon_1_author_name, anon_1.author_email AS anon_1_author_email, anon_1.authored_date AS anon_1_authored_date, anon_1.committer_name AS anon_1_committer_name, anon_1.commiter_email AS anon_1_commiter_email, anon_1.commited_date AS anon_1_commited_date 
FROM branches, LATERAL (SELECT commits.id AS id, commits.branch_id AS branch_id, commits.created_date AS created_date, commits.commit_id AS commit_id, commits.commit_short_id AS commit_short_id, commits.commit_message AS commit_message, commits.author_name AS author_name, commits.author_email AS author_email, commits.authored_date AS authored_date, commits.committer_name AS committer_name, commits.commiter_email AS commiter_email, commits.commited_date AS commited_date 
FROM commits 
WHERE commits.branch_id = branches.id ORDER BY commits.authored_date DESC 
 LIMIT %(param_1)s) AS anon_1
相关问题