without_distinct() and first() does not work right together. #92

Closed
socketpair opened this Issue Oct 27, 2014 · 7 comments

Projects

None yet

2 participants

@socketpair

working code:

a = select(t.field for t in T).order_by(...)
a.without_distinct()
a = a.first()

Not working codes:
a = select(t.field for t in T).without_distinct().order_by(...).first()
a = select(t.field for t in T).order_by(...).without_distinct().first()
a = select(t.field for t in T).order_by(...).first().without_distinct()

@kozlovsky
Contributor

Oops, I forgot that .without_dictinct() return query result instead of query. Probably should fix that

@kozlovsky kozlovsky self-assigned this Oct 27, 2014
@kozlovsky kozlovsky added the bug label Oct 27, 2014
@kozlovsky kozlovsky closed this in 5111574 Oct 28, 2014
@kozlovsky
Contributor

Now this should work:

a = select(t.field for t in T).without_distinct().order_by(...).first()
a = select(t.field for t in T).order_by(...).without_distinct().first()

But this should not:

a = select(t.field for t in T).order_by(...).first().without_distinct()

because the result of first() is not the new query, but the resulted object (or None)

The code that you wrote initially was incorrect:

a = select(t.field for t in T).order_by(...)
a.without_distinct()
a = a.first()

This doesn't work as expected, because the result of without_distinct() is ignored. Correct code should look like this:

query = select(t.field for t in T).order_by(...)
query = query.without_distinct()
obj = query.first()

But note that I just fixed #90 as well, so the explicit call of without_distinct() is now unnecessary here, and the code can be written as:

obj = select(t.field for t in T).order_by(...).first()

with the same effect.

@socketpair

Huge thanks for detailed description

@socketpair

Just leave it here:

query = select(t.field for t in T).order_by(...)
query = query.without_distinct()
obj = query.first()

generates:

AttributeError: 'QueryResult' object has no attribute 'first'
@kozlovsky
Contributor

Are you updated Pony from GitHub? It works just fine for me:

>>> from pony.orm.examples.presentation import *
>>> query = select(s for s in Student).order_by(Student.name)
>>> query = query.without_distinct()
>>> obj = query.first()
GET NEW CONNECTION
SWITCH TO AUTOCOMMIT MODE
SELECT "s"."id", "s"."name", "s"."dob", "s"."tel", "s"."gpa", "s"."group"
FROM "Student" "s"
ORDER BY "s"."name"
LIMIT 1

>>> obj
Student[3]
@socketpair

I mean that I cannot use .first() and .without_distinct() simultaneously in pony 0.5.4. I did not check in 0.6

@kozlovsky
Contributor

Probably I was not clear enough, the behavior I described is new behavior which I just committed to github. Before the fix .without_distinct() returned a list of objects, and now it returns a normal query, which can be refined further with .order_by(), .first(), etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment