[Sqlalchemy-tickets] Issue #3545: filter_by danger if accidentally using "and" (zzzeek/sqlalchemy)
Brought to you by:
zzzeek
|
From: Brian C. <iss...@bi...> - 2015-10-01 12:41:09
|
New issue 3545: filter_by danger if accidentally using "and" https://bitbucket.org/zzzeek/sqlalchemy/issues/3545/filter_by-danger-if-accidentally-using-and Brian Candler: There is some dangerous behaviour with filter_by which I think warrants a warning in the documentation. [Tested with SQLAlchemy 0.9.9] # WRONG Foo.filter_by(Foo.ref == ref and Foo.type == type and Foo.tag == tag) # RIGHT Foo.filter_by(Foo.ref == ref, Foo.type == type, Foo.tag == tag) Foo.filter_by((Foo.ref == ref) & (Foo.type == type) & (Foo.tag == tag)) from sqlalchemy import and_ Foo.filter_by(and_(Foo.ref == ref, Foo.type == type, Foo.tag == tag)) The incorrect form is silently accepted and apparently works, but actually behaves the same as Foo.filter_by(Foo.ref == ref) i.e. part of the query you expected to be generated is quietly dropped off. At http://docs.sqlalchemy.org/en/rel_1_0/orm/query.html it says: "Multiple criteria are joined together by AND:" followed by a valid example, but I think it should clarify that you don't actually use the python "and" operator. |