Opened 4 years ago

Closed 4 years ago

#31549 closed Bug (invalid)

Wrong results when executing QuerySet.query.

Reported by: Lawes Owned by: nobody
Component: Database layer (models, ORM) Version: 2.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Mariusz Felisiak)

We have a class:

from django.db.models import models
class MyModel(models.Model):
    import_at = models.DateField( db_index=True, null=True, default=None,)

then I can get some results by:

queryset = MyModel.objects.filter(import_at__month=5, import_at__year=import_at[0])
print(queryset.count())

It will print 4

but when I used connection, I got diffrent result:

from django.db import transaction, connection
with connection.cursor() as cursor:
    cursor.execute(queryset.query)
    print(len(cursor.fetchall()))

It will print 0

I got 0 !

If I using month or year, and get result by cursor.execute(queryset.query), we have diffrent result from connection.

Change History (2)

comment:1 by Lawes, 4 years ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 4 years ago

Description: modified (diff)
Resolution: invalid
Status: newclosed
Summary: Diffrent result from connectionWrong results when executing QuerySet.query.

queryset.query is a helper, it doesn't produce SQL that can be executed directly in a database console, see #25705.

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