#21903: Query with select_related and defer on MySQL causes id field to be 
returned
as bool
-------------------------------+--------------------
     Reporter:  matklad        |      Owner:  nobody
         Type:  Bug            |     Status:  new
    Component:  Uncategorized  |    Version:  1.6
     Severity:  Normal         |   Keywords:
 Triage Stage:  Unreviewed     |  Has patch:  0
Easy pickings:  0              |      UI/UX:  0
-------------------------------+--------------------
 I selected an object `obj` by a query with select_related and defer, to
 find out that `obj.user.id` is `True` instead of `1`. Technically, `True
 == 1`, but it doesn't work with, for example, url reversing. I managed to
 reproduce this behaviour on MySQL, python2 and python3. With SQLite I get
 `1` as expected. Here is a test case that show this surprising behaviour:

 models.py
 {{{
 from django.conf import settings
 from django.db import models

 class A(models.Model):
     dspam = models.TextField()
     eggs = models.BooleanField(default=False)

 class B(models.Model):
     a = models.ForeignKey(A)

 class C(models.Model):
     b = models.ForeignKey(B)

 class D(models.Model):
     c = models.ForeignKey(C)
     dfoo = models.TextField()
     dbar = models.TextField()

     user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True)
     time = models.DateTimeField(null=True)

 class E(models.Model):
     d = models.ForeignKey(D)
     dbaz = models.TextField()
 }}}
 test.py
 {{{
 from django.contrib.auth.models import User
 from django.test import TestCase
 from .models import *

 class Test(TestCase):
     def setUp(self):
         self.a = A.objects.create()
         self.b = B.objects.create(a=self.a)
         self.c = C.objects.create(b=self.b)
         self.user = User.objects.create_user("foo", "foo....@example.com",
 password="424242", id=1)
         self.d = D.objects.create(c=self.c, user=self.user)
         self.e = E.objects.create(d=self.d)

     def test(self):
         qs = (E.objects
               .select_related('d__user', 'd__c__b__a')
               .filter(d__c__b=self.b, d__user_id=1)
               .defer('dbaz', 'd__dfoo', 'd__dbar',
                      'd__c__b__a__dspam'))
         user = qs[0].d.user
         print(user.id)  # prints True. Technically it is an Int, but bools
 don't work with, for example, url reversing
 }}}

 pip freeze

 {{{
 Django==1.6.1
 MySQL-python==1.2.5
 wsgiref==0.1.2
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/21903>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.39ea66720fe8a448784ed6608c5a4723%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to