Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"unknown column X.id" error in django using existing DB

Tags:

python

django

I am trying to create a model for an existsing DB. Using the output of manage.py inspectdb, My models.py file looks like this:

from django.db import models

...some more stuff here...

class Scripts(models.Model):
    run_site = models.ForeignKey(Sites, db_column='run_site')
    script_name = models.CharField(max_length=120)
    module_name = models.CharField(unique=True, max_length=120)
    type = models.CharField(max_length=24)
    cat_name = models.CharField(max_length=90)
    owner = models.ForeignKey(QAPeople, db_column='owner')
    only_server = models.CharField(max_length=120, blank=True)
    guest = models.IntegerField()
    registered = models.IntegerField()
    super = models.IntegerField()
    admin = models.IntegerField()
    run_timing = models.CharField(max_length=27)
    manual_owner = models.ForeignKey(QAPeople, db_column='manual_owner')
    script_id = models.IntegerField(unique=True,)
    version = models.IntegerField()
    comment = models.ForeignKey('ScriptComments', null=True, blank=True)
    class Meta:
        db_table = u'scripts'

When I try to do Scripts.objects.all() I get

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python26\lib\site-packages\django\db\models\query.py", line 68, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "C:\Python26\lib\site-packages\django\db\models\query.py", line 83, in __len__
    self._result_cache.extend(list(self._iter))
  File "C:\Python26\lib\site-packages\django\db\models\query.py", line 269, in iterator
    for row in compiler.results_iter():
  File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 672, in results_iter
    for rows in self.execute_sql(MULTI):
  File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 727, in execute_sql
    cursor.execute(sql, params)
  File "C:\Python26\lib\site-packages\django\db\backends\util.py", line 15, in execute
    return self.cursor.execute(sql, params)
  File "C:\Python26\lib\site-packages\django\db\backends\mysql\base.py", line 86, in execute
    return self.cursor.execute(query, args)
  File "C:\Python26\lib\site-packages\MySQLdb\cursors.py", line 173, in execute
    self.errorhandler(self, exc, value)
  File "C:\Python26\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
OperationalError: (1054, "Unknown column 'scripts.id' in 'field list'")

Why does django think there should be a scripts.id column? How do I fix it without dropping the tables etc?

like image 276
olamundo Avatar asked Nov 16 '10 08:11

olamundo


1 Answers

There is always by default an implicit id field as auto incrementing primary key on every model. See primary_key in the Django docs how to change that field to some other name, but there needs to be one primary key (also in your table).

Also you may not want to call one of your fields super, since it is shadowing Python's built-in super in the class body. Might give you a hard time finding a bug some day.

like image 131
stefanw Avatar answered Oct 07 '22 03:10

stefanw