I followed the advice in this question but I'm still receiving this error:
sqlalchemy.exc.NoForeignKeysError: Can't find any foreign key relationships between 'investment' and 'real_estate'.
Models
class Investment(db.Model):
__tablename__ = 'investment'
__mapper_args__ = {'polymorphic_identity': 'investment'}
id = Column(db.Integer, primary_key=True)
item_name = Column(db.String(64))
investment_type = Column(db.String(32), nullable=False)
__mapper_args__ = {'polymorphic_on': investment_type}
class RealEstate(Investment):
__tablename__ = 'real_estate'
__mapper_args__ = {'polymorphic_identity': 'real_estate'}
id = Column(db.Integer, primary_key=True)
current_balance = Column(db.Float)
I also updated the examples in that answer to reflect changes in SQLAlchemy's docs but, still, the error.
The goal here is that I don't want the attributes inherited by RealEstate to be in the Investment table. I would like to inherit and retain all of these attributes with the RealEstate table.
Can anyone see what I missed?
The id
attribute in RealEstate must be a foreign key referencing the Investment. Otherwise, how can you say that every RealEstate is an investment ?
Change your RealEstate class definition to the following:
class RealEstate(Investment):
__tablename__ = 'real_estate'
__mapper_args__ = {'polymorphic_identity': 'real_estate'}
# This id below should reference Investment. Hence, define it as foreign key.
id = Column(Integer, ForeignKey('investment.id'), primary_key=True)
current_balance = Column(db.Float)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With