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

Comments

Projects
None yet
2 participants
@socketpair

socketpair commented Oct 27, 2014

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

This comment has been minimized.

Show comment
Hide comment
@kozlovsky

kozlovsky Oct 27, 2014

Member

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

Member

kozlovsky commented Oct 27, 2014

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

This comment has been minimized.

Show comment
Hide comment
@kozlovsky

kozlovsky Oct 28, 2014

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.

Member

kozlovsky commented Oct 28, 2014

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

This comment has been minimized.

Show comment
Hide comment
@socketpair

socketpair Oct 28, 2014

Huge thanks for detailed description

socketpair commented Oct 28, 2014

Huge thanks for detailed description

@socketpair

This comment has been minimized.

Show comment
Hide comment
@socketpair

socketpair Oct 28, 2014

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'

socketpair commented Oct 28, 2014

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

This comment has been minimized.

Show comment
Hide comment
@kozlovsky

kozlovsky Oct 28, 2014

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]
Member

kozlovsky commented Oct 28, 2014

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

This comment has been minimized.

Show comment
Hide comment
@socketpair

socketpair Oct 29, 2014

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

socketpair commented Oct 29, 2014

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

@kozlovsky

This comment has been minimized.

Show comment
Hide comment
@kozlovsky

kozlovsky Oct 29, 2014

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.

Member

kozlovsky commented Oct 29, 2014

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