Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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

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

socketpair opened this issue Oct 27, 2014 · 7 comments
Assignees
Labels

Comments

@socketpair
Copy link

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
Copy link
Member

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
Copy link
Member

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
Copy link
Author

Huge thanks for detailed description

@socketpair
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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

@kozlovsky
Copy link
Member

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
Labels
Projects
None yet
Development

No branches or pull requests

2 participants