RowProxy don't get sorted as expected

Issue #2848 resolved
Former user created an issue

If one uses raw queries like this:

res = list(session.execute("SELECT a,b FROM my_subscription"))

then we see (by printing) that res is [13209L), (1L, 7057L), (2L, 410L), (10L, 3L), (6L, 10L)]((0L,)

But these aren't tuples, so when we try to sort() that list (or use sorted() on it), records just get weirdly mixed up.

I guess RowProxy objects should have the same behaviour as tuples when it comes to sorting, or else they should display themselves as objects instead of tuples, to avoid confusing unexperienced users.

Comments (3)

  1. Mike Bayer repo owner

    patch:

    diff --git a/lib/sqlalchemy/engine/result.py b/lib/sqlalchemy/engine/result.py
    index 0e23165..93c7d3b 100644
    --- a/lib/sqlalchemy/engine/result.py
    +++ b/lib/sqlalchemy/engine/result.py
    @@ -125,8 +125,11 @@ class RowProxy(BaseRowProxy):
    
         __hash__ = None
    
    +    def __lt__(self, other):
    +        return tuple(self) < tuple(other)
    +
         def __eq__(self, other):
    -        return other is self or other == tuple(self)
    +        return other is self or tuple(other) == tuple(self)
    
         def __ne__(self, other):
             return not self.__eq__(other)
    
  2. Log in to comment