繁体   English   中英

如何访问sqlalchemy中的孩子object

[英]how to access the child object in sqlalchemy

有人可以帮忙吗

subscription = session.get(SubscriptionModel, 1)
print(subscription.customer)

这会打印客户的 ID,但我如何获得客户 object?

您必须定义您的数据 model 以包含与另一个 class 的关系。应该这样做

class subscription (Base):
    __tablename__ = 'subscription'
    id = Column(Integer, primary_key=True)
    customer_id = Column(Integer, ForeignKey('subscription_model.id'))

    customer = relationship("SubscriptionModel", foreign_keys=customer_id )

然后你将拥有 object 而不是 id:

subscription = session.query(SubscriptionModel).get(1)
print(subscription.customer_id) # ID
print(subscription.customer)    # Object

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM