Issues

Issue #3083 resolved

select_from() no longer sets _select_from_entity

Sebastian Bank
created an issue

When using filter_by to simplify filter expressions

session.query(User).filter(User.id == 1)
session.query(User).filter_by(id=1)

where filter_by applies to the last join entity

session.query(User).join(user_group).filter(user_group.c.group_id == 5)
session.query(User).join(user_group).filter_by(group_id=5)

I stumbled on the fact that it does not follow a select_from (which I expected intuitively):

session.query(User).select_from(user_group).filter(user_group.c.group_id == 5).join(User)
session.query(User).select_from(user_group).filter_by(group_id=5).join(User)

The second one fails. In my concrete case, the primary entity actually had an attribute with the same name. Hence, I ended up with the wrong filter condition (discovering that much later).

Maybe a hint in the documentation of filter_by and/or select_from could help so that others don't make the same mistake.

In the long run, I might also suggest to change this behaviour.

Comments (7)

  1. Mike Bayer repo owner

    this is a critical regression from 0.8, broken in 04317bd5 due to the change for #2736. While that ticket is correct, the _select_from_entity attribute was totally forgotten and no tests caught it.

    Two such tests that should pass are below.

    from sqlalchemy import *
    from sqlalchemy.orm import *
    from sqlalchemy.ext.declarative import declarative_base
    
    Base = declarative_base()
    
    class A(Base):
        __tablename__ = 'a'
    
        id = Column(Integer, primary_key=True)
        bs = relationship("B")
    
    
    class B(Base):
        __tablename__ = 'b'
    
        id = Column(Integer, primary_key=True)
        a_id = Column(Integer, ForeignKey('a.id'))
        a = relationship("A")
    
    sess = Session()
    
    print sess.query(A).select_from(B).join("a")
    
    print sess.query(A).select_from(B).filter_by(a_id=5).join(A)
    
  2. Sebastian Bank reporter

    Aah, allright, thanks. I first thought it was a bug. But after reading in the docs that filter_by applies to the primary query entity or the last join call, it looked more like intended behaviour.

    I may have misinterpreted the terminology (does select_from reset the primary entity?). Maybe the docs on that can be made more helpful when the bug is fixed.

  3. Mike Bayer repo owner
    • Fixed a regression from 0.9.0 due to ticket2736 where the :meth:.Query.select_from method no longer set up the "from entity" of the :class:.Query object correctly, so that subsequent :meth:.Query.filter_by or :meth:.Query.join calls would fail to check the appropriate "from" entity when searching for attributes by string name. fixes #3083

    → <<cset ec40a84d96e4>>

  4. Mike Bayer repo owner
    • Fixed a regression from 0.9.0 due to ticket2736 where the :meth:.Query.select_from method no longer set up the "from entity" of the :class:.Query object correctly, so that subsequent :meth:.Query.filter_by or :meth:.Query.join calls would fail to check the appropriate "from" entity when searching for attributes by string name. fixes #3083

    → <<cset 87a4ebf8af84>>

  5. Log in to comment