Backref relationships don't populate in the class until instance is created #7313
-
Describe the bugReferencing a backref relationship raises an AttributeError unless an instance is instantiated first. This causes a problem when the first thing you do is try to use a 2.0-style select() query using one of the relationships in a condition. It is easy enough to workaround, but the relationship should exist at the start. To Reproducefrom sqlalchemy import *
from sqlalchemy.orm import *
Base = declarative_base()
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
children = relationship('Child', backref='parent')
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
parentid = Column(Integer, ForeignKey('parent.id'))
# Uncommenting this makes the relationship available.
# c = Child()
Child.parent Error
Versions
Additional contextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi, This seems due to the lazy initialization of the modules. You should place a call to The suggested alternative is to avoid backref and use instead backpopulate with an explicit relationship. |
Beta Was this translation helpful? Give feedback.
Hi,
This seems due to the lazy initialization of the modules. You should place a call to
configure_mappers()
one they are all imported. I don't think there is much we can do without breaking other use cases here.The suggested alternative is to avoid backref and use instead backpopulate with an explicit relationship.