我有很多关系,我试图查询返回一个或所有对象,但我能得到的最大值是该对象的id,它是我的Association Class Table的外键。
这是我的关联类表
class PolPropAssociation(db.Model):
__tablename__ = 'polprop'
politician = db.Column(db.Integer, db.ForeignKey('politics.idPolitician'), primary_key=True)
proposal = db.Column(db.Integer, db.ForeignKey('proposals.idProposal'), primary_key=True)
relation = db.Column(db.String(120))
parent = db.relationship("Politic", back_populates="children")
child = db.relationship("Proposal", back_populates="parents")
draft = db.Column(db.Boolean, default=True)
def __init__(self, relation):
self.relation = relation.title()
class Politic(db.Model):
__searchable__ = ['publicName', 'completeName']
__tablename__ = 'politics'
idPolitician = db.Column(db.Integer, primary_key=True)
publicName = db.Column(db.String(150))
completeName = db.Column(db.String(300))
startDate = db.Column(db.Date, default=datetime.datetime.utcnow)
endDate = db.Column(db.Date, default=datetime.datetime.utcnow)
positions=db.relationship('Position',backref='pos_politic',lazy='select')
draft = db.Column(db.Boolean, default = True)
biolink = db.Column(db.String(200))
flag = db.relationship('Flag', cascade="all, delete", backref='politics', lazy='dynamic')
children = db.relationship("PolPropAssociation", back_populates="parent", lazy='dynamic')
class Proposal(db.Model):
__tablename__ = 'proposals'
idProposal = db.Column(db.Integer, primary_key=True)
dateProposal = db.Column(db.Date, default=datetime.datetime.utcnow)
description = db.Column(db.String(500))
linkProposal = db.Column(db.String(200))
idCategory = db.Column(db.Integer, db.ForeignKey('category.idcategory'))
idProposalState = db.Column(db.Integer, db.ForeignKey('proposalstate.idproposalstate'))
draft = db.Column(db.Boolean, default = True)
flag = db.relationship('FlagProposal', backref='proposals', lazy='dynamic')
parents = db.relationship("PolPropAssociation", back_populates="child", lazy='dynamic')
我想要回复一位政治家提出的所有建议,并提出idProposal:
proposal = Proposal.query.filter_by(idProposal=idProposal).first()
politicians = proposal.parents.all()
但它返回的是一个包含我的关联表的所有政治家列的数组。有什么方法可以用这些ID返回 Politic 对象吗?
祝你好运
答案 0 :(得分:0)
我找到了解决方案:
如果有人询问如何使用对象关联表查询特定内容,请按照以下步骤进行:
pol = Politic.query.filter(Politic.children.any(proposal=idProposal)).all()