Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlalchemy.exc.InvalidRequestError: Mapper '...' has no property '...'

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.

like image 625
nven Avatar asked Feb 09 '17 14:02

nven


2 Answers

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.

like image 163
Yauhen Avatar answered Nov 04 '22 08:11

Yauhen


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

like image 37
nven Avatar answered Nov 04 '22 08:11

nven