Varolan bir DB için bir model oluşturmaya çalışıyorum. manage.py inspectdb
çıktısını kullanarak, My models.py
dosya şuna benzer:Varolan DB'yi kullanarak django'daki "unknown column X.id" hatası
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'
ben Scripts.objects.all()
yapmaya çalıştığımda
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'")
Neden Django bir scripts.id
sütunun olması gerektiğini düşünüyor
Bu nedenle 'model.pk' işlevini 'model.id' yerine ana anahtara başvurmak iyi bir alışkanlıktır. –