使用南迁移时出现Django错误:“字段列表”中的未知列

时间:2011-07-27 19:47:34

标签: python django django-south

使用schemamigration将列添加到数据库时,我遇到了问题。有问题的字段'is_flagged'是属于我的应用'upload'中的视频模型的布尔值。运行迁移时,我得到以下内容:

....:~/..../webapp$ python manage.py schemamigration upload --auto
Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_manager(settings)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 379, 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 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 219, in execute
    self.validate()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 249, in validate
    num_errors = get_validation_errors(s, app)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/validation.py", line 36, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 146, in get_app_errors
    self._populate()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 61, in _populate
    self.load_app(app_name, True)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 78, in load_app
    models = import_module('.models', app_name)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/..../score/models.py", line 43, in <module>
    class Score(models.Model):          # matches with one specific user and one specific video
  File "/home/..../score/models.py", line 45, in Score
    video = models.ForeignKey(Video, default=Video.objects.all()[0])    # default value shouldn't end up in real objects
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 190, in __getitem__
    return list(qs)[0]
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 84, in __len__
    self._result_cache.extend(self._iter)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 273, in iterator
    for row in compiler.results_iter():
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 680, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 735, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 34, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 86, in execute
    return self.cursor.execute(query, args)
  File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py", line 166, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/pymodules/python2.7/MySQLdb/connections.py", line 35, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1054, "Unknown column 'upload_video.is_flagged' in 'field list'")

知道它为什么不让我迁移?当我特意使用--add-field时也会发生同样的情况。

我认为问题可能在于另一个app中的ForeignKey(正如你在traceback中看到的那样“得分”)引用了Video对象。如果是这种情况,那么迁移其他应用程序中ForeignKeys引用的模型的正确方法是什么?

更新

我找到了一个解决方法,即在“得分”中注释违规的ForeignKey行,像往常一样运行迁移,最后取消注释该行,而不是自己迁移“得分”。这可行,但它不优雅,如果应用程序之间有很多ForeignKeys会很麻烦。正如我之前所问,有没有办法自动避免这种情况,而不必使用ForeignKey将模型编辑到带有新字段的模型?

2 个答案:

答案 0 :(得分:5)

问题不在于您有ForeignKey指向要迁移的模型。这是一种非常常见的情况,而South肯定可以解决这个问题。 真正的问题是你在FK声明中做了一些非常非常糟糕的事情。

video = models.ForeignKey(Video, default=Video.objects.all()[0])

此默认声明将在导入时执行查询并获取其第一个值。在导入时执行的事情是一个很大的禁忌(特别是查询)。 因此,当South导入模型以便它可以内省它们并生成模式迁移时,会发生Video.objects.all()。但是因为Video模型被更改了(你刚刚添加了一个新字段),但在DB中还没有,Django的ORM会引发错误。

如果您确实想将其设置为默认值,请使用可调用的

def get_default_video():
     return Video.objects.all()[0]

...
video = models.ForeignKey(Video, default=get_default_video)

注意:虽然我首先想到一个很好的理由,但是将第一个Video记录设置为视频FK的默认值。你想做什么?

答案 1 :(得分:5)

不确定这是否正是您正在寻找的但我遇到了类似的问题,这就是修复它的原因。如果您更改了模型并且没有正确更新数据库,那么您将得到相同的错误。运行syncdb不会更改已创建的表,因此在加载任何其他数据之前必须重置数据库。

python manage.py reset (app_name)

然后再次同步你的数据库,你应该很好。

python manage.py syncdb