Mehdi Selbi
Mehdi Selbi

Reputation: 147

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'")

Upvotes: 0

Views: 1214

Answers (3)

A.Zahid
A.Zahid

Reputation: 1

class Images(models.Model):
    name = models.CharField(max_length=100)
    id = models.CharField(max_length=100, primary_key=True)
    image = models.FileField()

def __str__(self):
    return self.name

Set primary_key=True which field you want to set as the primary key.

Upvotes: 0

Mehdi Selbi
Mehdi Selbi

Reputation: 147

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.

Upvotes: 1

Pushprajsinh Chudasama
Pushprajsinh Chudasama

Reputation: 7949

$ python manage.py makemigrations

$ python manage.py migrate

run following command in order for changes to be applied to the database.

Upvotes: 0

Related Questions