Calling bake_lazy_loaders() can raise IndexError

Issue #3612 resolved
Eben Freeman created an issue

Hi! Apologies in advance if I'm missing something really obvious here. SQLAlchemy 1.0's BakedQuery feature is amazing! But I'm running into problems upon attempting to invoke bake_lazy_loaders() as described here. Here's an example (tested with SQLAlchemy 1.0.10 and 1.1.0b1.dev0):

from sqlalchemy import Column, Integer, ForeignKey, create_engine
from sqlalchemy.orm import Session, relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.baked import bake_lazy_loaders

bake_lazy_loaders()

Base = declarative_base()


class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)


class Address(Base):
    __tablename__ = 'address'
    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey('user.id'))
    user = relationship("User", lazy='joined')


engine = create_engine('sqlite:///:memory:')
Base.metadata.create_all(engine)

session = Session(engine)
u = session.query(User).first()

This produces the following exception

  File "testcase.py", line 28, in <module>
    u = session.query(User).first()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1272, in query
    return self._query_cls(entities, self, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 110, in __init__
    self._set_entities(entities)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 120, in _set_entities
    self._set_entity_selectables(self._entities)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 150, in _set_entity_selectables
    ent.setup_entity(*d[entity])
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 3417, in setup_entity
    self._with_polymorphic = ext_info.with_polymorphic_mappers
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/langhelpers.py", line 747, in __get__
    obj.__dict__[self.__name__] = result = self.fget(obj)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 1893, in _with_polymorphic_mappers
    configure_mappers()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 2756, in configure_mappers
    mapper._post_configure_properties()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 1713, in _post_configure_properties
    prop.post_instrument_class(self)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/interfaces.py", line 526, in post_instrument_class
    self.strategy.init_class_attribute(mapper)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/strategies.py", line 1096, in init_class_attribute
    _get_strategy_by_cls(LazyLoader).init_class_attribute(mapper)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/interfaces.py", line 496, in _get_strategy_by_cls
    return self._get_strategy(cls._strategy_keys[0])
IndexError: list index out of range

It seems that the same exception is raised when the relationship is defined without lazy='joined', if bake_lazy_loaders() is invoked after the class mapping is defined.

Is this a bug, or am I misusing bake_lazy_loaders?

Comments (8)

  1. Mike Bayer repo owner
    • Fixed bug in baked loader system where the systemwide monkeypatch for setting up baked lazy loaders would interfere with other loader strategies that rely on lazy loading as a fallback, e.g. joined and subquery eager loaders, leading to IndexError exceptions at mapper configuration time. fixes #3612

    → <<cset a22b2085068f>>

  2. Mike Bayer repo owner
    • Fixed bug in baked loader system where the systemwide monkeypatch for setting up baked lazy loaders would interfere with other loader strategies that rely on lazy loading as a fallback, e.g. joined and subquery eager loaders, leading to IndexError exceptions at mapper configuration time. fixes #3612

    (cherry picked from commit a22b2085068f860d05dbb98631d2ac2079a12b39)

    → <<cset c3876fe755e0>>

  3. Eben Freeman reporter

    Wow, thank you for the amazingly rapid response! One question -- is the pdb.set_trace() stanza supposed to be in that patch? (guess it probably doesn't matter)

  4. Log in to comment