[Sqlalchemy-tickets] Issue #4336: BooleanClauseList.compare fails when an expression uses func.cast
Brought to you by:
zzzeek
From: miso <iss...@bi...> - 2018-09-17 18:02:16
|
New issue 4336: BooleanClauseList.compare fails when an expression uses func.cast https://bitbucket.org/zzzeek/sqlalchemy/issues/4336/booleanclauselistcompare-fails-when-an miso: Hello. I am using the method [compare](https://docs.sqlalchemy.org/en/latest/core/sqlelement.html?highlight=compare#sqlalchemy.sql.expression.ClauseList.compare) of some classes to perform unit test on a function that creates query expressions dynamically. Two identical expressions fail to compare when **func.cast** is used, even when their string representations are exactly the same. Here I am leaving a script to test this issue. In the last line, the compare method returns **False**. ``` #!python from sqlalchemy import func, Column, Integer, String, Text from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.sql.expression import or_ Base = declarative_base() class Cartoon(Base): __tablename__ = 'cartoon' id = Column(Integer, primary_key=True) name = Column(String) nickname = Column(String) age = Column(Integer) # Create two identical expressions A = or_(Cartoon.name.ilike('%black%'), Cartoon.nickname.ilike('%black%')) B = or_(Cartoon.name.ilike('%black%'), Cartoon.nickname.ilike('%black%')) assert str(A) == str(B) # Ok assert A.compare(B) # Ok # Create two identical expressions using cast C = or_(Cartoon.name.ilike('%black%'), func.cast(Cartoon.age, Text).ilike('%black%')) D = or_(Cartoon.name.ilike('%black%'), func.cast(Cartoon.age, Text).ilike('%black%')) assert str(C) == str(D) # The string expressions are equal assert C.compare(D) # Here the comparison fails ``` |