#27277: Error in generation of SQL query when have a ForeignKey with Model with
ChoiceField
-------------------------------------+-------------------------------------
     Reporter:  Cátia Simões         |      Owner:  nobody
         Type:  Bug                  |     Status:  new
    Component:  Database layer       |    Version:  1.9
  (models, ORM)                      |   Keywords:  SQL, QUERYSET,
     Severity:  Release blocker      |  CHOICEFIELD
 Triage Stage:  Unreviewed           |  Has patch:  0
Easy pickings:  0                    |      UI/UX:  0
-------------------------------------+-------------------------------------
 I have a model with a choicefield (Customers.gender) and other with a
 ForeignKey for this (Orders.customer)

 Because of choicefield i can't do something like
 Orders.objects.filter(customer=customer) I get a error OperationalError:
 (1054, "Unknown column 'yourstory_customers.gender' in 'field list'")

 More details:

 DataBase: MySQL

 Model:


 {{{
 class Customers(models.Model):
     id = models.AutoField(db_column='ID', primary_key=True)
     user = models.OneToOneField(User, db_column='USER_ID', null=True,
 blank=True, default=None)  # maybe null
     register_language = models.ForeignKey(Languages,
 db_column='LANGUAGE_ID', null=True)
     name = models.CharField(db_column='NAME', max_length=400)

     gender = models.IntegerField(db_column='GENDER', choices=GENDER,
 default=0)
     ...


 class Orders(models.Model):
     id = models.AutoField(db_column='ID', primary_key=True)
     order_number = models.IntegerField(db_column='ORDER_NUMBER',
 unique=True)
     efective_number = models.IntegerField(db_column='EFECTIVE_NUMBER',
 null=True)
     payment_date = models.DateTimeField(db_column='PAYMENT_DATE',
 blank=True, null=True)
     payment_method = models.CharField(db_column='PAYMENT_METHOD',
 max_length=400, blank=True, null=True)
     customer = models.ForeignKey('Customers', db_column='CUSTOMER_ID')
     nif = models.CharField(db_column='NIF', max_length=200, null=True,
 blank=True)
     billing_address = models.ForeignKey('Addresses',
 db_column='ORDER_BILLING_ADDRESS_ID', related_name='billing_address',
                                         blank=True, null=True)
     ...

 }}}
 Queryset:

 {{{
 Orders.objects.filter(customer=customer)
 }}}


 Error:
 OperationalError: (1054, "Unknown column 'yourstory_customers.gender' in
 'field list'")

 Generated query:

 {{{
 SELECT (( case  when yourstory_customers.gender= "0" then "Male" when
 yourstory_customers.gender= "1" then "Female" else "" end )) AS `gender`,
 `yourstory_orders`.`ID`, `yourstory_orders`.`ORDER_NUMBER`,
 `yourstory_orders`.`EFECTIVE_NUMBER`, ... FROM `yourstory_orders` WHERE
 `yourstory_orders`.`CUSTOMER_ID` = 1016 ORDER BY
 `yourstory_orders`.`ORDER_NUMBER` ASC LIMIT 1
 }}}


 I need to change my queryset , see below, to force customer table added in
 FROM instruction to this work.

 {{{
 Orders.objects.filter(customer=customer, customer__gender__in=[0, 1])
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27277>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.7a740334cfeba94e4c860296c4c4ed84%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to