auto-correlation regression regarding eager load, column prop, eg. the usual suspects

Issue #2530 resolved
Mike Bayer repo owner created an issue

appeared some time in 0.7:

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class SnortEvent(Base):
    __tablename__ = "event"

    id = Column(Integer, primary_key=True)
    signature = Column(Integer, ForeignKey("signature.id"))

    signatures = relationship("Signature", lazy=False)

class Signature(Base):
    __tablename__ = "signature"

    id = Column(Integer, primary_key=True)

    sig_count = column_property(
                    select([func.count('*')](func.count('*'))).\
                        where(SnortEvent.signature == id)
                )

# workaround
#Signature.sig_count = column_property(
#                    select([func.count('*')](func.count('*'))).\
#                        where(SnortEvent.signature == Signature.id).\
#                        correlate(Signature.__table__)
#                )

session = Session()

print session.query(SnortEvent)



sqlalchemy.exc.InvalidRequestError: Select statement 'SELECT count(:param_1) AS count_1 
FROM event, signature AS signature_1 
WHERE event.signature = signature_1.id' returned no FROM clauses due to auto-correlation; specify correlate(<tables>) to control correlation manually.

Comments (4)

  1. Mike Bayer reporter

    yeah so if you just undo the smallest part of #2261:

    --- a/lib/sqlalchemy/sql/expression.py  Thu Jul 05 10:19:59 2012 -0400
    +++ b/lib/sqlalchemy/sql/expression.py  Sun Jul 08 14:58:55 2012 -0400
    @@ -4807,7 +4807,7 @@
    
             _SelectBase.__init__(self, **kwargs)
    
    -    @property
    +    @util.memoized_property
         def _froms(self):
             # would love to cache this,
             # but there's just enough edge cases, particularly now that
    

    then it's fine. but it's really working by accident there.

  2. Mike Bayer reporter

    OK, I think really we just need to tell people they need to set up correlate() or correlate_except() here. correlate_except() is easier, so 0.8 only, and then its even better if they can just pass the mapped class in, which is #2245. So will update #2245 with this documentation case.

  3. Log in to comment