#31549: Wrong results when executing QuerySet.query.
-------------------------------------+-------------------------------------
Reporter: MrLawes | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):
* status: new => closed
* resolution: => invalid
Old description:
> 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.
New description:
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.
--
Comment:
`queryset.query` is a helper, it doesn't produce SQL that can be executed
directly in a database console, see #25705.
--
Ticket URL: <https://code.djangoproject.com/ticket/31549#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.69e1c103ef02d3dc94b6ee64b0d0c2ef%40djangoproject.com.