attribute.get() invokes __eq__() on object returned by many-to-one lazyload

Issue #2260 resolved
Former user created an issue

This was originally reported against SQLSoup, but the issue is really inappropriate call of __eq__():

        class NoEqAllowed(object):
            def __eq__(self, other):
                raise Exception("nope")

        addresses, users = self.tables.addresses, self.tables.users
        Address = self.classes.Address

        mapper(NoEqAllowed, users, properties={
            'addresses':relationship(Address, backref='user')
        })
        mapper(Address, addresses)

        u1 = NoEqAllowed()
        u1.name = "some name"
        u1.addresses = [email_address='a1')](Address(id=12,)
        s = Session(testing.db)
        s.add(u1)
        s.commit()

        a1 = s.query(Address).filter_by(id=12).one()
        assert a1.user is u1



Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nose/case.py", line 187, in runTest
    self.test(*self.arg)
  File "/Users/classic/dev/sqlalchemy/./test/orm/test_mapper.py", line 714, in test_we_dont_call_eq
    assert a1.user is u1
  File "/Users/classic/dev/sqlalchemy/./lib/sqlalchemy/orm/attributes.py", line 168, in __get__
    return self.impl.get(instance_state(instance),dict_)
  File "/Users/classic/dev/sqlalchemy/./lib/sqlalchemy/orm/attributes.py", line 424, in get
    if value in (PASSIVE_NO_RESULT, NEVER_SET):
  File "/Users/classic/dev/sqlalchemy/./test/orm/test_mapper.py", line 696, in __eq__
    raise Exception("nope")
Exception: nope

patch

diff -r 07179d2aae12bb4e72eb1e494a870eefada8320a lib/sqlalchemy/orm/attributes.py
--- a/lib/sqlalchemy/orm/attributes.py  Tue Sep 06 09:51:18 2011 -0400
+++ b/lib/sqlalchemy/orm/attributes.py  Fri Sep 09 15:43:51 2011 -0400
@@ -421,7 +421,7 @@
                 else:
                     value = ATTR_EMPTY

-                if value in (PASSIVE_NO_RESULT, NEVER_SET):
+                if value is PASSIVE_NO_RESULT or value is NEVER_SET :
                     return value
                 elif value is ATTR_WAS_SET:
                     try:

Comments (8)

  1. Mike Bayer repo owner

    it would be extremely helpful if you could, as suggested in the guidelines, to attach a succinct test case which reproduces the issue. Otherwise any developer who wants to evaluate this bug must spend potentially hours attempting to reproduce the conditions that you have in front of you.

  2. Former user Account Deleted

    Sorry for not attaching the testcase previously. It is now attached and you can run it.

  3. Mike Bayer repo owner
    • changed milestone to 0.7.3

    its a good looking test case, thanks for the effort.

    I'll take a look at this soon.

  4. Log in to comment