在模型中禁用SQL炼金术关系
我有一个名为Hospital的Sqlalchemy模型,该模型将其映射到DB中有各种领域的桌子上。我还有另一张名为“动物”的桌子,一家医院在逻辑上包含动物,所以我在医院模型中有这样的关系:
animals = relationship("Animal", back_populates="animal", lazy=True)
现在,我的存储库中有很多代码可以进入医院。动物症,因为它在某一时刻很有用。但是,现在我不再需要它,我想要医院。动物可以解决到空列表,以便我现有的代码可以继续引用医院。动物和返回[]。是否有一种简单的方法将上述关系转换/禁用将返回[]的事物?我尝试将动物变成财产等,但行不通。
I have a SQLAlchemy model called Hospital that maps to a table in the DB with various fields. I also have another table called Animal and a Hospital logically contains Animals so I have a relationship like this in the Hospital model:
animals = relationship("Animal", back_populates="animal", lazy=True)
Now there is a lot of code in my repo that accesses the hospital.animals because it was useful at one point. However now I no longer need it and I wanted hospital.animals to resolve to an empty list so that my existing code can continue to reference hospital.animals and return []. Is there an easy way to convert/disable the relationship above to something that would return a []? I tried turning animals into a property etc but it does not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除
Animal
Delete
animals = relationship("Animal", back_populates="animal", lazy=True)
and add code below to your Hospital model