Search code examples
djangopython-3.xdjango-modelsdjango-orminspectdb

Django unknown column error when trying to get a model objects


I'm trying to set up a web-app that modify an existing MySQL database using Dajngo, the model for my table below was generated via Django inspectdb:

class BaseCase(models.Model):
    base_case_name = models.TextField(blank=True, null=True)
    version = models.TextField(blank=True, null=True)
    default = models.TextField(blank=True, null=True)  # This field type is a guess.
    class Meta:
        managed = False
        db_table = 'base_case'

and here is an SQL of that base_case table in the database :

CREATE TABLE `base_case` (
  `base_case_name` tinytext,
  `version` tinytext,
  `default` bit(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

the problem is when I try to get the objects via Django ORM I keep getting this error

django.db.utils.OperationalError: (1054, "Unknown column 'base_case.id' in 'field list'")

Solution

  • I solved the problem by defining a primary key in the BaseCase Table, Django try to match the primary key column to its own generated id (base_case.id) in the field list.