Opened 5 years ago

Closed 5 years ago

Last modified 3 years ago

#12079 closed (fixed)

queryset.exists() doesn’t work correctly

Reported by: ramusus Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc: ramusus@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I don't understand is it normal?

    In [17]: User.objects.filter(pk=0)
    Out[17]: []

    In [18]: User.objects.filter(pk=0).exists()
    Out[18]: True

The exists() should return False result in this case, I think. This is revision 11653 (Development version)

I have related problem with unique validating of inlineformset and I found that the reason is queryset.exists() doesn't work correctly

Attachments (1)

12079.diff (513 bytes) - added by kmtracey 5 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 Changed 5 years ago by ramusus

  • Cc ramusus@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 5 years ago by Alex

This works correctly for me.

>>> from django.contrib.auth.models import  User

>>> User.objects.filter(pk=0)
[2] []

>>> User.objects.filter(pk=0).exists()
[3] False

Can you provide some more information about your setup, such as db backend?

comment:3 Changed 5 years ago by kmtracey

  • Triage Stage changed from Unreviewed to Accepted

It's failing on MySQL, at least. This code:

http://code.djangoproject.com/browser/django/trunk/django/db/models/sql/query.py#L2381

simply returns result when the connection can use chunked reads. For mysql result is a callable-interator and bool(<callable-interator>) is True.

Changed 5 years ago by kmtracey

comment:4 Changed 5 years ago by kmtracey

I've attached a possible patch but I don't have much of a clue when it comes to the ORM so someone with more of one in that area should see if there's a better way, etc.

comment:5 Changed 5 years ago by kmtracey

(No tests needed for this fix...a lookup test added in r11647 is currently failing on backends where this problem occurs.)

comment:6 Changed 5 years ago by ramusus

Can you provide some more information about your setup, such as db backend?

DATABASE_ENGINE = 'postgresql_psycopg2'

comment:7 Changed 5 years ago by Alex

Karen, that patch looks correct. I didn't notice this as Jacob committed a different version than my patch (which included a call to list()).

comment:8 Changed 5 years ago by Alex

  • Has patch set
  • milestone set to 1.2
  • Triage Stage changed from Accepted to Ready for checkin
  • Version changed from 1.1 to SVN

comment:9 Changed 5 years ago by kmtracey

  • Resolution set to fixed
  • Status changed from new to closed

(In [11654]) Fixed #12079: Changed has_results to get a single result, thus preventing exists() from always returning True on backends that support chunked reads.

comment:10 Changed 3 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

Note: See TracTickets for help on using tickets.
Back to Top