我的数据库中有一个简单的插入问题(如本post中所述)。 我问了一个新问题,因为问题有点不同。
我的模型和sql表是相同的,我尝试插入一行指定了一个字段,它返回一个错误,说它不知道另一个字段..
我的sql表如下所示:
FeuilleTemps | CREATE TABLE `FeuilleTemps` (
`f_id` int(11) NOT NULL AUTO_INCREMENT,
`f_id_user` int(11) DEFAULT NULL,
`f_date` date DEFAULT NULL,
`f_id_proj` int(11) DEFAULT NULL,
`f_comment` longtext,
`f_id_task` int(11) DEFAULT NULL,
`f_unite` int(11) DEFAULT NULL,
`f_absent` int(11) DEFAULT NULL,
PRIMARY KEY (`f_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
然后我打电话给inspectdb --database ='myDb'> models.py
在models.py中我有这个:
class Feuilletemps(models.Model):
f_id = models.IntegerField(primary_key=True)
f_id_user = models.IntegerField(null=True, blank=True)
f_date = models.DateField(null=True, blank=True)
f_id_proj = models.IntegerField(null=True, blank=True)
f_comment = models.TextField(blank=True)
f_id_task = models.IntegerField(null=True, blank=True)
f_unite = models.IntegerField(null=True, blank=True)
f_absent = models.IntegerField(null=True, blank=True)
class Meta:
db_table = u'FeuilleTemps'
然后我尝试进行以下插入查询:
f = Feuilletemps(f_id_user=1085)
f.save()
它给了我这个错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.6/site-packages/django/db/models/base.py", line 460, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/usr/lib/python2.6/site-packages/django/db/models/base.py", line 553, in save_base
result = manager._insert(values, return_id=update_pk, using=using)
File "/usr/lib/python2.6/site-packages/django/db/models/manager.py", line 195, in _insert
return insert_query(self.model, values, **kwargs)
File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 1436, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 791, in execute_sql
cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql
cursor.execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/util.py", line 34, in execute
return self.cursor.execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 86, in execute
return self.cursor.execute(query, args)
File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 173, in execute
self.errorhandler(self, exc, value)
File "/usr/lib64/python2.6/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
OperationalError: (1054, "Unknown column 'f_id_proj' in 'field list'")
答案 0 :(得分:1)
好的,我终于明白了。 我需要打电话
f.save(using='myDb')
语法的这种变化有点令人困惑,因为我通常使用类似
的东西Feuilletemps.objects.using('myDb').get(f_id=1)