Property all_orm_descriptors doesn't contain backref attributes
Issue #3486
resolved
I don't know if its desired behaviour but currently the Mapper.all_orm_descriptors
doesn't contain relationship backref attributes. Atleast I think the docs should clearly state that all_orm_descriptors
doesn't contain these. Consider the following model definition:
import sqlalchemy as sa
Base = sa.ext.declarative.declarative_base()
group_user = sa.Table(
'group_user',
Base.metadata,
sa.Column('user_id', sa.Integer, sa.ForeignKey('user.id')),
sa.Column('group_id', sa.Integer, sa.ForeignKey('group.id'))
)
class Group(Base):
__tablename__ = 'group'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String)
class User(Base):
__tablename__ = 'user'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String)
groups = sa.orm.relationship(
'Group',
secondary=group_user,
backref='users'
)
User.__mapper__.attrs.keys()
# ['groups', 'id', 'name']
Group.__mapper__.attrs.keys()
# ['users', 'id', 'name']
User.__mapper__.all_orm_descriptors.keys()
# ['groups', 'id', 'name', '__mapper__']
Group.__mapper__.all_orm_descriptors.keys()
# ['id', 'name', '__mapper__'] <- Doesn't contain 'users'
I have some situations where I need to inspect all model descriptors. Currently I need to use all_orm_descriptors and combine those with backref relationships of given model.
Comments (3)
-
repo owner -
reporter Ah sorry this was my bad.
configure_mappers()
did the job. -
reporter - changed status to resolved
- Log in to comment
has to be there. call configure_mappers() first. that backrefs aren't there until this happens is expected behavior, favor back_populates over backref.