Opened 8 years ago

Closed 8 years ago

#27277 closed Bug (invalid)

Error in generation of SQL query when have a ForeignKey with Model with ChoiceField

Reported by: Cátia Simões Owned by: nobody
Component: Database layer (models, ORM) Version: 1.9
Severity: Normal Keywords: SQL, QUERYSET, CHOICEFIELD
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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])

Change History (2)

comment:1 by Tim Graham, 8 years ago

Severity: Release blockerNormal

Can you please edit your models to form a minimal example to reproduce? For example, currently you have undefined references to Languages and GENDER which make trying to reproduce this more difficult. I'm surprised if Django is generating case / when SQL for the queryset you gave. Are you sure about this? Are you using django.db.backends.mysql in the DATABASES setting?

comment:2 by Cátia Simões, 8 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top