gpt4 book ai didi

python - sqlalchemy:如何返回连接 3 个表的对象列表?

转载 作者:行者123 更新时间:2023-11-29 10:09:09 24 4
gpt4 key购买 nike

@hybrid_method
# @paginate
def investors(self, **kwargs):
"""All investors for a given Custodian"""
ind_inv_type_id = InvestorType.where(description="Individual").first().id
inv_query = Investor.with_joined(InvestorAddress, InvestmentAddress, CustodianAddress) \
.filter_by(custodians_id=self.id) \
.with_joined(Investment) \
.filter_by(investor_types_id=ind_inv_type_id)
investors = Investor.where(None, False, inv_query, **kwargs)

temp_inv_query = Investor.with_joined(CustodianInvestor, Custodian)\
.filter_by(Custodian.id==self.id)
temp_investors = Investor.where(None, False, temp_inv_query, **kwargs)
return list(set(investors + temp_investors))
# end def investors



# @auth.access_controlled
class InvestorAddress(db.Model, EntityAddressMixin):
# Metadata
__tablename__ = 'investor_addresses'

# Database Columns
investors_id = db.Column(db.ForeignKey("investors.investors_id"),
nullable=False)
investor = db.relationship("Investor", foreign_keys=[investors_id],
backref=db.backref("InvestorAddress"))

# end class InvestorAddress

class InvestmentAddress(db.Model):
"""This model differs from other EntityAddress Models because it links to either an investor_address or an custodian_address."""

# Metadata
__tablename__ = 'investment_addresses'

# Database Columns
address_types_id = db.Column(
db.ForeignKey("address_types.address_types_id"),
nullable=False)
address_type = db.relationship("AddressType",
foreign_keys=[address_types_id],
backref=db.backref("InvestmentAddress"))

investments_id = db.Column(db.ForeignKey("investments.investments_id"),
nullable=False)
investment = db.relationship("Investment",
foreign_keys=[investments_id],
backref=db.backref("InvestmentAddress"))

investor_addresses_id = db.Column(db.ForeignKey(
"investor_addresses.investor_addresses_id"))
investor_address = db.relationship("InvestorAddress",
foreign_keys=[investor_addresses_id],
backref=db.backref("InvestmentAddress"))

custodian_addresses_id = db.Column(db.ForeignKey(
"custodian_addresses.custodian_addresses_id"))
custodian_address = db.relationship("CustodianAddress",
foreign_keys=[custodian_addresses_id],
backref=db.backref("InvestmentAddress")
)
# end class InvestmentAddress



class CustodianAddress(db.Model, EntityAddressMixin):
"""Defines the relationship between a Custodian and their addresses."""

# Metadata
__tablename__ = 'custodian_addresses'

# Database Columns
custodians_id = db.Column(db.ForeignKey(
"custodians.custodians_id"), nullable=False)
custodian = db.relationship("Custodian", foreign_keys=[custodians_id],
backref=db.backref("CustodianAddress"))

# end CustodianAddress

我有一个应用程序,此函数应该返回给定“托管人”的“投资者”列表。现在,当它执行时,我收到错误:“sqlalchemy.exc.ArgumentError:映射器选项需要字符串键或属性列表”。该错误来自“inv_query”中的“join”。我已经包含了我用于连接的 3 个模型。

最佳答案

如您提供的文档中所述。 here

您应该在 with_joined 中提供字符串参数(表名称)。鉴于您已经定义了关系

Investor.with_joined('investorAddressTable', 'investmentAddressTable, 'custodianAddressTable')

如果你可以使用 session ,那么你可以直接查询 ORM 类

session.query(Investor).join(InvestorAddress).join(InvestmentAddress).join(CustodianAddress).all() # will assume you have set the foreign key properly

关于python - sqlalchemy:如何返回连接 3 个表的对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51330300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com