There is an info table and it has relationship to car table or suvtable.
Itis specified in info.type field.
So how can I create association on fly based on the type data of the record?
class Info(Base):
item_id = Column(ForeignKey('cars-or-suvs-table.id'))
type = Column(String())
class Car(Base):
- data -
info = relationship('Info', backref="car")
class Suv(Base):
- data -
info = relationship('Info', backref="suv")
Edit: I already have the tables filled with data, so I can not change db schema.
Since you’re looking for a solution that doesn’t require moving the foreign key to a different table, you can try this approach:
I renamed Info.car to Info._car since ._car will unavoidably be a bogus car object even if .type is ‘suv’.
I’ve left the event listener stuff out to keep it simple, but you can definitely adapt what pieces you need from my other answer to avoid things getting into an inconsistent state.