【问题标题】:Django - OperationalError: (1054, "Unknown column 'xx' in 'field list'")Django - OperationalError:(1054,“'字段列表'中的未知列'xx'”)
【发布时间】:2013-12-10 08:18:43
【问题描述】:

我的数据库中的简单插入有问题(如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'")

【问题讨论】:

  • 是你自己创建的表还是使用了django syncdb/migrate 命令?
  • 我在我的mysql数据库端创建了表,然后我使用inspectdb和syncdb在django中自动创建模型
  • 你是什么意思创建了一个表然后使用了syncdb? syncdb 正是为您的应用程序创建必要表的原因。也许您应该首先删除数据库并从 syncdb 开始。
  • 不,我使用了 inspectdb,我只是做了过去 2 年我为所有表所做的事情,而且工作得很好。与这个唯一不同的是它在另一个数据库中。

标签: mysql django django-models django-queryset


【解决方案1】:

好吧,我终于想通了。 我需要打电话

f.save(using='myDb')

语法上的这种变化有点令人困惑,因为我通常使用类似的东西

Feuilletemps.objects.using('myDb').get(f_id=1)

【讨论】:

  • 我遇到了和你一样的问题,但对于遗留数据库来说,这对我来说并不奏效。
猜你喜欢
  • 2016-04-05
  • 1970-01-01
  • 2017-06-16
  • 2016-09-29
  • 1970-01-01
  • 1970-01-01
  • 2020-01-23
  • 1970-01-01
  • 2018-03-12
相关资源
最近更新 更多