WHERE 0 = 1 silently generated

Issue #3033 invalid
Shoe created an issue

Running:

query = session.query(A).filter( and_( A.id == string_a, A.b.key == string_b ) )

where:

class A(Base):
    __tablename__   = 'a_table'
    id              = Column(Integer, primary_key = True)
    # ...

class B(Base):
    __tablename__       = 'b_table'
    id                  = Column(Integer, primary_key = True)
    key = Column(String(36), nullable = False)
    # ...
    a_id = Column(Integer, ForeignKey('a_table.id'), nullable = False)
    a = relationship("A", backref = backref("b", uselist = False))

produces a query like:

SELECT ... 
FROM a_table
WHERE 0 = 1

Comments (6)

  1. Mike Bayer repo owner

    The "A.b.key" use here is invalid. You mean to say this:

    session.query(A).join(A.b).filter(A.id == "x").filter(B.key == "y")
    
  2. Mike Bayer repo owner

    the query as you have it is essentially this:

    session.query(A).filter(A.b.key == "x")
    

    which is:

    session.query(A).filter("b" == "x")
    

    which is:

    session.query(A).filter(False)
    

    hence 1 == 0. SQLAlchemy is working correctly.

  3. Log in to comment