[Django] #31549: Diffrent result from connection

9 views
Skip to first unread message

Django

unread,
May 8, 2020, 5:39:59 AM5/8/20
to django-...@googlegroups.com
#31549: Diffrent result from connection
-------------------------------------+-------------------------------------
Reporter: MrLawes | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 2.0
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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())
>>> 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()))

>>> 0

I got 0 !

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

--
Ticket URL: <https://code.djangoproject.com/ticket/31549>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 8, 2020, 5:40:48 AM5/8/20
to django-...@googlegroups.com
#31549: Diffrent result from connection
-------------------------------------+-------------------------------------
Reporter: MrLawes | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by MrLawes:

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())
> >>> 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()))
>
> >>> 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.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/31549#comment:1>

Django

unread,
May 8, 2020, 6:04:45 AM5/8/20
to django-...@googlegroups.com
#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>

Reply all
Reply to author
Forward
0 new messages