django.db.utils.OperationalError:(1054,“字段列表'中的未知列'zoneclient_user.last_sync_time'”)

时间:2017-12-21 14:52:05

标签: django bash unit-testing

如果我运行makemigrations,那很好。但是,如果我决定在bash中运行迁移,那么我得到了以下回溯。我将在相关模型下面显示

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 71, in execute
    super(Command, self).execute(*args, **options)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 88, in handle
    failures = test_runner.run_tests(test_labels)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/test/runner.py", line 147, in run_tests
    old_config = self.setup_databases()
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/test/runner.py", line 109, in setup_databases
    return setup_databases(self.verbosity, self.interactive, **kwargs)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/test/runner.py", line 299, in setup_databases
    serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/db/backends/creation.py", line 377, in create_test_db
    test_flush=True,
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 115, in call_command
    return klass.execute(*args, **defaults)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 165, in handle
    emit_post_migrate_signal(created_models, self.verbosity, self.interactive, connection.alias)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/core/management/sql.py", line 276, in emit_post_migrate_signal
    db=db)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 198, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/guardian/management/__init__.py", line 31, in create_anonymous_user
    User.objects.get(pk=guardian_settings.ANONYMOUS_USER_ID)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/db/models/query.py", line 351, in get
    num = len(clone)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/db/models/query.py", line 122, in __len__
    self._fetch_all()
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/db/models/query.py", line 966, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/db/models/query.py", line 265, in iterator
    for row in compiler.results_iter():
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 701, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 787, in execute_sql
    cursor.execute(sql, params)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 129, in execute
    return self.cursor.execute(query, args)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/home/infinity/.virtualenvs/p38-1/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
django.db.utils.OperationalError: (1054, "Unknown column 'zoneclient_user.last_sync_time' in 'field list'")

这是我模特的一部分

class User(AbstractUser):

   ...
    last_sync_time = models.DateTimeField(
        _('Last sync time'), blank=True, null=True)

    @property
    def is_synch(self):
        now = datetime.datetime.now()
        if not self.last_sync_time:
            return False
        elif self.last_sync_time < (now + datetime.timedelta(minutes=5)):
            return False
        else:
            return True

我该如何解决这个问题?我认为问题出在DateTimeField上,但到目前为止还不清楚。

P.S。请注意,我正在使用Django 1.7.11和python 2.7.4。

1 个答案:

答案 0 :(得分:0)

回溯显示当django-guardian尝试创建匿名用户时发生错误。

User.objects.get(pk=guardian_settings.ANONYMOUS_USER_ID)

让它发挥作用的黑客方法可能是暂时从您的INSTALLED_APPS删除django监护人,直到迁移用户模型为止。