Mapper property all_orm_descriptors memoized even after new attributes get added

Issue #3778 resolved
Konsta Vesterinen created an issue

The following code should outline the issue:

import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base, synonym_for

Base = declarative_base()


class Article(Base):
    __tablename__ = 'article'
    id = sa.Column(sa.Integer, primary_key=True)


Article.__mapper__.all_orm_descriptors

Article.name = sa.Column(sa.String)

Article.__mapper__.all_orm_descriptors.keys()

I would expect the last line to return a list containing 'name', but currently it only returns ['id', '__mapper__']. Even calling sa.orm.configure_mappers() won't help here.

I noticed this problem when I had SQLAlchemy mapper configuration listeners that were accessing Mapper.all_orm_descriptors.

Comments (5)

  1. Mike Bayer repo owner

    patch (let me know if it works)

    diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py
    index b76a6f7..40e963c 100644
    --- a/lib/sqlalchemy/orm/mapper.py
    +++ b/lib/sqlalchemy/orm/mapper.py
    @@ -2099,7 +2099,7 @@ class Mapper(InspectionAttr):
                         continue
                     yield c
    
    -    @util.memoized_property
    +    @_memoized_configured_property
         def attrs(self):
             """A namespace of all :class:`.MapperProperty` objects
             associated this mapper.
    @@ -2137,7 +2137,7 @@ class Mapper(InspectionAttr):
                 configure_mappers()
             return util.ImmutableProperties(self._props)
    
    -    @util.memoized_property
    +    @_memoized_configured_property
         def all_orm_descriptors(self):
             """A namespace of all :class:`.InspectionAttr` attributes associated
             with the mapped class.
    
  2. Konsta Vesterinen reporter

    Yeah it works! Thanks for very fast response Mike =)

    Edit: it works after sa.orm.configure_mappers() is called. Just to clarify if anyone else is encountering this issue.

  3. Mike Bayer repo owner

    Use configured props for mapper.attrs, mapper.all_orm_descriptors

    Fixed bug where the :attr:.Mapper.attrs, :attr:.Mapper.all_orm_descriptors and other derived attributes would fail to refresh when mapper properties or other ORM constructs were added to the mapper/class after these accessors were first called.

    (also trying different ways to get the changelog to merge cleanly)

    Change-Id: Iaecdb4b3d8c3a3b44302a5880476e60a1f4e27d9 Fixes: #3778

    → <<cset f2eb4aac9517>>

  4. Mike Bayer repo owner

    Use configured props for mapper.attrs, mapper.all_orm_descriptors

    Fixed bug where the :attr:.Mapper.attrs, :attr:.Mapper.all_orm_descriptors and other derived attributes would fail to refresh when mapper properties or other ORM constructs were added to the mapper/class after these accessors were first called.

    Change-Id: Iaecdb4b3d8c3a3b44302a5880476e60a1f4e27d9 Fixes: #3778 (cherry picked from commit 6319eb0ce7c095ba7d4f60746ce12cf575730b46)

    → <<cset 8b64240e3a2c>>

  5. Log in to comment