[英]Disabling SQL alchemy relationship in a model
I have a SQLAlchemy model called Hospital that maps to a table in the DB with various fields.我有一个名为 Hospital 的 SQLAlchemy 模型,它映射到数据库中具有各种字段的表。 I also have another table called Animal and a Hospital logically contains Animals so I have a relationship like this in the Hospital model:
我还有另一个名为 Animal 的表,Hospital 逻辑上包含 Animals,所以我在 Hospital 模型中有这样的关系:
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.现在我的 repo 中有很多代码可以访问 hospital.animals,因为它在某一时刻很有用。 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 [].
但是现在我不再需要它,我希望将 hospital.animals 解析为一个空列表,以便我现有的代码可以继续引用 hospital.animals 并返回 []。 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.
我尝试将动物变成财产等,但它不起作用。
Delete animals = relationship("Animal", back_populates="animal", lazy=True)
and add code below to your Hospital model删除
animals = relationship("Animal", back_populates="animal", lazy=True)
并将下面的代码添加到您的 Hospital 模型中
@property
def animals(self):
return []
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.