[Sqlalchemy-tickets] Issue #3310: Relationship __ne__ does not handle aliased() properly (zzzeek/sq
Brought to you by:
zzzeek
|
From: Brandon C. <iss...@bi...> - 2015-02-20 18:47:13
|
New issue 3310: Relationship __ne__ does not handle aliased() properly https://bitbucket.org/zzzeek/sqlalchemy/issue/3310/relationship-__ne__-does-not-handle Brandon Clarke: Found from SO post [Here](https://stackoverflow.com/questions/28610563/sqlalchemy-different-output-on-queries-field-is-null-and-is-not-null-on-ali). Sample code to reproduce: from sqlalchemy import * from sqlalchemy.orm import aliased from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import relation, sessionmaker, relationship, backref Base = declarative_base() class Parent(Base): __tablename__ = 'parent' id = Column(Integer, primary_key=True) children = relation('Child', back_populates='parents') class Child(Base): __tablename__ = 'child' id = Column(Integer, primary_key=True) parent_id = Column(Integer, ForeignKey('parent.id')) parents = relation('Parent', back_populates='children', uselist=False) aChild = aliased(Child) Session = sessionmaker() Session = Session() print Session.query(aChild.id).filter(~(aChild.parents == None)) """ SELECT children_1.id AS children_1_id FROM children AS children_1 WHERE children_1.parent_id IS NOT NULL """ print Session.query(aChild.id).filter(aChild.parents != None) #failing case """ SELECT children_1.id AS children_1_id FROM children AS children_1, children WHERE children.parent_id IS NOT NULL """ I did some poking around and have a proposed fix, below: @@ -1291,8 +1291,8 @@ class RelationshipProperty(StrategizedProperty): """ if isinstance(other, (util.NoneType, expression.Null)): if self.property.direction == MANYTOONE: - return sql.or_(*[x != None for x in - self.property._calculated_foreign_keys]) + return _orm_annotate(~self.property._optimized_compare( + None, adapt_source=self.adapter)) I basically used the logic in place for __eq__ and inverted it. It passes all existing test cases, I'm just working on creating a test case for this failure mode. I was planning on wrapping up a pull request today. If there are any suggestions for where to locate the test, please let me know. First time submitting anything here, so if I'm doing anything wrong, please let me know that too |