Search code examples
pythonsqlalchemysqlalchemy-migrate

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. 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.


Solution

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

    @property
    def animals(self):
        return []