when in try to access mysql view using django model it throws an error

by mageshwaran   Last Updated August 14, 2019 18:26 PM

I want to access the records in mysql view using django filter but it throws an error

class PartnerPayBillSummaryDetails(models.Model):
    invoice_no = models.CharField(max_length=200)
    invoice_id = models.IntegerField()
    partner_id = models.IntegerField()
    partner_name = models.CharField(max_length=100)
    status = models.CharField(null=False, max_length=50)
    invoice_date = models.DateField()
    bill_received_date = models.DateField()
    due_date = models.DateField()
    due_date_ageing = models.IntegerField()
    payable_amount = models.DecimalField(max_digits=20, decimal_places=2, default=0)

    class Meta:
        managed = False
        db_table = '66_1_partner_pay_bill_ageing_dtl'
try:
    p = PartnerPayBillSummaryDetails.objects.all()
    print(p)
except Exception as e:
    print(e)

Error:

OperationalError: (1054, "Unknown column '66_1_partner_pay_bill_ageing_dtl.id' in 'field list'")


Answers 3


I figured out the answer

it requires a filed as a primary key. I solve this by adding "primary_key=True" in invoice_id

invoice_id = models.IntegerField(primary_key=True)
mageshwaran
mageshwaran
August 05, 2019 11:33 AM

In my opinion it is looking for a primary key called 'id' (by default if you don't define). Also your db_table name in meta class is starting with a number. Not sure it will allow that. I am away from computer to test it out but based on guess, I have two solutions for you. Please try them in that order. All I have done is change your model class in both the solutions.

class PartnerPayBillSummaryDetails(models.Model):
    invoice_no = models.CharField(max_length=200)
    invoice_id = models.IntegerField()
    partner_id = models.IntegerField()
    partner_name = models.CharField(max_length=100)
    status = models.CharField(null=False, max_length=50)
    invoice_date = models.DateField()
    bill_received_date = models.DateField()
    due_date = models.DateField()
    due_date_ageing = models.IntegerField()
    payable_amount = models.DecimalField(max_digits=20, decimal_places=2, default=0)

    class Meta:
        managed = False
        db_table = 'partner_pay_bill_ageing_dtl_66_1'

Solution 2

class PartnerPayBillSummaryDetails(models.Model):
    id = models.AutoField(primary_key=True)
    invoice_no = models.CharField(max_length=200)
    invoice_id = models.IntegerField()
    partner_id = models.IntegerField()
    partner_name = models.CharField(max_length=100)
    status = models.CharField(null=False, max_length=50)
    invoice_date = models.DateField()
    bill_received_date = models.DateField()
    due_date = models.DateField()
    due_date_ageing = models.IntegerField()
    payable_amount = models.DecimalField(max_digits=20, decimal_places=2, default=0)

    class Meta:
        managed = False
        db_table = 'partner_pay_bill_ageing_dtl_66_1'

Please let me know if there is still some error.

Amit
Amit
August 05, 2019 11:44 AM

You have to add primary key to the model.

id = models.AutoField(primary_key=True)

or make any column the primary key like:

invoice_id = models.IntegerField(primary_key=True)
Shaheer Raza
Shaheer Raza
August 05, 2019 12:07 PM

Related Questions



Django - backend logic vs database logic

Updated June 04, 2017 11:26 AM

django.db.utils.ProgrammingError: Table doesn't exist

Updated February 26, 2018 13:26 PM

Group the django object according to the foreign key

Updated October 18, 2018 08:26 AM

Django best practice for join mysql tables

Updated April 27, 2018 21:26 PM