I have two classes, TrialIdentifier
and TimeCourse
TimeCourse has an instance variable containing a TrialIdentifier, and I am trying to setup a foreign key relationship between the two.
in TrialIdentifier
__tablename__ = 'trial_identifiers'
relationships = relationship('TimeCourse',
back_populates = 'trial_identifier', uselist = False)
in TimeCourse
__tablename__ = 'time_course'
trial_identifier_id = Column(Integer, ForeignKey('trial_identifiers.id'))
trial_identifier = relationship('TrialIdentifier', back_populates = 'relationships')`
If I name the variable trial_identifier
in TimeCourse
the following error is thrown:
sqlalchemy.exc.InvalidRequestError: Mapper 'Mapper|TimeCourse|time_course' has no property 'trial_identifier'
If I name it anything else, everything works fine. The entire package is built on the TimeCourse().trial_identifier
so I would like to avoid refactoring it if possible. Or at least understand this behavior.
Also, you can use an alternative way to solve this problem. Just use backref
instead of back_populates
. It works for me:
In TimeCourse:
trial_identifier = relationship('TrialIdentifiers', backref = 'time_course')
More info you can find at SQLAlchemy docs.
trial_identifier was a private variable, defined as _trial_identifier
Either removing the setters and getters, or replacing
trial_identifier = relationship('TrialIdentifier', back_populates = 'relationships')`
with
_trial_identifier = relationship('TrialIdentifier', back_populates = 'relationships')`
solved the problem
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