1. Michael Bayer
  2. sqlalchemy
  3. Issues

Issues

Issue #3033 invalid

WHERE 0 = 1 silently generated

Jefffrey
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. Michael 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.

  2. Log in to comment