ORM Query: Comparing a relation(al) attribute of a mapped class to null() fails with traceback

Issue #1415 resolved
Former user created an issue

Assume that an attribute ref of a mapped class User corresponds to a foreign key of the (corresponding) table user. Obviously, the attribute is created as a relation(...).

Assume further that we create a query on User and try to filter on some condition involving NULL, e.g. session.query(User).filter_by(ref=null()) or session.query(User).filter(User.ref == null()). This fails with a traceback.

Workarounds: Use the underlying foreign key column (if it is mapped) instead of ref or compare to None instead of null().

The attached file should work if you fill in an appropriate engine. It prduced the following output:

Traceback (most recent call last): File "sqlatest.py", line 24, in ? print session.query(User).filter(User.ref == null()).all() File "/home/barthelmannk/PortaleHeadZope/sandbox/eggs/ SQLAlchemy-0.5.4p1-py2.4.egg/sqlalchemy/sql/expression.py", line 1253, in eq return self.operate(operators.eq, other) File "/home/barthelmannk/PortaleHeadZope/sandbox/eggs/ SQLAlchemy-0.5.4p1-py2.4.egg/sqlalchemy/orm/attributes.py", line 120, in operate return op(self.comparator, other, *kwargs) File "/home/barthelmannk/PortaleHeadZope/sandbox/eggs/ SQLAlchemy-0.5.4p1-py2.4.egg/sqlalchemy/orm/properties.py", line 492, in eq return _orm_annotate(self.property._optimized_compare(other, adapt_source=self.adapter)) File "/home/barthelmannk/PortaleHeadZope/sandbox/eggs/ SQLAlchemy-0.5.4p1-py2.4.egg/sqlalchemy/orm/properties.py", line 623, in _optimized_compare value = attributes.instance_state(value) AttributeError: '_Null' object has no attribute '_sa_instance_state'

Comments (5)

  1. Mike Bayer repo owner

    you should be comparing to None. I think I want to change null() to just return None in 0.6....

  2. Log in to comment