SQLAlchemy过滤器相关的子查询

时间:2016-12-05 22:59:54

标签: python flask sqlalchemy flask-sqlalchemy

class Poll(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255), unique=False)
    is_deleted = db.Column(db.Boolean, default=False)
    created_date = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    answers = db.relationship('Answer', backref='poll')

class Answer(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    poll_id = db.Column(db.Integer, db.ForeignKey(Poll.id), nullable=False)
    title = db.Column(db.String(255), unique=False, nullable=False)
    votes = db.Column(db.Integer, nullable=False)
    is_deleted = db.Column(db.Boolean, default=False, nullable=False)
    created_date = db.Column(db.DateTime, default=datetime.datetime.utcnow)

poll = PollModel.query.join(
    PollModel.answers
).options(
    subqueryload(PollModel.answers.query)
).filter(
    PollModel.id == poll_id,
    PollModel.is_deleted == False,
)[0]

我试图通过" is_deleted"来过滤我子查询的相关答案。属性。但我最后只是过滤了民意调查是否有一个未删除的答案。如何通过子查询过滤相关对象。

1 个答案:

答案 0 :(得分:0)

这会有用吗?

lsblk