Skip to content Skip to sidebar Skip to footer

Do All Relationships Modeled In Sqlalchemy Have To Be Bi-directional?

I am learning python and sqlalchemy and modelled this relationship between shops and locale. I get the error: InvalidRequestError: One or more mappers failed to initialize - can't

Solution 1:

SQLAlchemy ORM relationships are not required to be bi-directional. If using back_populates argument you are declaring it as such though. Using back_populates requires that you declare the other end as well:

Takes a string name and has the same meaning as backref, except the complementing property is not created automatically, and instead must be configured explicitly on the other mapper. The complementing property should also indicate back_populates to this relationship to ensure proper functioning.

(Latter emphasis mine)

Since you've not declared the property at the other end, SQLAlchemy complains. Just remove the back_populates argument:

classShop(maria.Base):
    ...
    country =relationship("Locale")
    ...

Post a Comment for "Do All Relationships Modeled In Sqlalchemy Have To Be Bi-directional?"