在模型中禁用SQL炼金术关系

发布于 2025-01-25 02:02:30 字数 340 浏览 1 评论 0原文

我有一个名为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_蜘蛛 2025-02-01 02:02:30

删除Animal

@property
def animals(self):
    return []

Delete animals = relationship("Animal", back_populates="animal", lazy=True) and add code below to your Hospital model

@property
def animals(self):
    return []
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文