Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

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:Python26libsite-packagesdjangodbmodelsquery.py", line 68, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "C:Python26libsite-packagesdjangodbmodelsquery.py", line 83, in __len__
    self._result_cache.extend(list(self._iter))
  File "C:Python26libsite-packagesdjangodbmodelsquery.py", line 269, in iterator
    for row in compiler.results_iter():
  File "C:Python26libsite-packagesdjangodbmodelssqlcompiler.py", line 672, in results_iter
    for rows in self.execute_sql(MULTI):
  File "C:Python26libsite-packagesdjangodbmodelssqlcompiler.py", line 727, in execute_sql
    cursor.execute(sql, params)
  File "C:Python26libsite-packagesdjangodbackendsutil.py", line 15, in execute
    return self.cursor.execute(sql, params)
  File "C:Python26libsite-packagesdjangodbackendsmysqlase.py", line 86, in execute
    return self.cursor.execute(query, args)
  File "C:Python26libsite-packagesMySQLdbcursors.py", line 173, in execute
    self.errorhandler(self, exc, value)
  File "C:Python26libsite-packagesMySQLdbconnections.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...