当没有这样的字段时,字段'blah_1'没有默认值

时间:2015-07-08 19:14:32

标签: python django

我正在尝试使用Django .save()方法将模型类型Apple保存到Django ORM(MySQL后端)。但是,我得到一个“字段'blah_1'没有默认值”错误。奇怪的是,对于Apple对象,没有'blah_1'字段,我认为我也没有在任何地方分配到这个不存在的字段。 一个'blah_fk_1'字段,但我确信没有'blah_1'字段。什么可能导致这个问题?

编辑:我只能提供这么多细节,有些代码很敏感。但是,我可以验证'blah_1' in apple._meta.get_all_field_names()(其中apple是Apple对象)产生False;即'blah_1'字段不存在。我还可以验证,如果您尝试分配给不存在的字段,则会出现AttributeError:'Apple'对象没有属性'blah_1'。由于没有出现这个错误,我知道我在任何时候都没有分配到不存在的字段。

编辑2:这是追溯(使用pdb时有一些额外的错误):

:Traceback (most recent call last):
  File "/usr/lib/python2.7/pdb.py", line 1314, in main
    pdb._runscript(mainpyfile)
  File "/usr/lib/python2.7/pdb.py", line 1233, in _runscript
    self.run(statement)
  File "/usr/lib/python2.7/bdb.py", line 400, in run
    exec cmd in globals, locals
  File "<string>", line 1, in <module>
  File "manage.py", line 2, in <module>
    import os
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "DRP/management/commands/make_apples.py", line 14, in handle
    create_new_apples(lab_group, debug=debug, bare_debug=debug)
  File "DRP/apple/apple.py", line 880, in create_new_apples
    store_new_Apple_list(lab_group, [[conf]+rec], debug=debug)
  File "DRP/database_construction.py", line 188, in store_new_Apple_list
    new_rec.save() #Store this apple in the database
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 545, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 573, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 654, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 687, in _do_insert
    using=using, raw=raw)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 232, in _insert
    return insert_query(self.model, objs, fields, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 1511, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 898, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 69, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 124, in execute
    return self.cursor.execute(query, args)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 176, in execute
    if not self._defer_warnings: self._warning_check()
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 92, in _warning_check
    warn(w[-1], self.Warning, 3)
Warning: Field 'blah_1' doesn't have a default value

编辑3:所以即使代码中没有引用该字段,但在我编写此代码之前的某个时间,检查MySQL数据库时该列仍然存在于数据库中。我该怎么办?

1 个答案:

答案 0 :(得分:1)

似乎,模型中字段super(),但未从数据库层正确删除。

您可以尝试查找删除字段的提交(如果您使用的是VCS),可能还包含迁移。

您还可以尝试运行blah_1以创建迁移,或write it yourself