在不存在的模型字段上获取迁移错误

时间:2018-06-07 21:21:05

标签: python django

今天跑过我的服务器,意识到我有两次“未应用的迁移”。我运行命令makemigrations并且它没有检测到任何变化,所以我自然然后运行了迁移。

我在终端上收到此错误代码:

Applying Campaigns.0016_campaignrecruit_date...Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", l
ine 371, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", l
ine 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line
288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line
335, in execute
    output = self.handle(*args, **options)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/commands/migrat
e.py", line 200, in handle
    fake_initial=fake_initial,
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/executor.py", lin
e 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/executor.py", lin
e 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/executor.py", lin
e 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/migration.py", li
ne 122, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/operations/fields
.py", line 84, in database_forwards
    field,
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py",
 line 306, in add_field
    self._remake_table(model, create_field=field)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py",
 line 178, in _remake_table

    self.effective_default(create_field)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/base/schema.py", li
ne 240, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/__init__.py",
line 767, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/__init__.py",
line 1276, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/__init__.py",
line 1271, in get_prep_value
    return self.to_python(value)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/__init__.py",
line 1233, in to_python
parsed = parse_date(value)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/dateparse.py", line 74, i
n parse_date
    match = date_re.match(value)
TypeError: expected string or bytes-like object

所以对我来说,看起来我的模型campaignrecruit中的日期字段正在获得一个它不知道如何处理的输入。我会理解的。但问题是我的campaignrecruit模型中没有日期字段。时间字段现在有意注释掉。

这是我的models.py:

class campaignrecruit(models.Model):
    campaign = models.ForeignKey(startcampaign, on_delete=models.CASCADE)
    volunteer = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    join = models.BooleanField()
    #Time = models.IntegerField()
    def __str__(self):
        return str(self.volunteer)

还有什么会引发此错误?任何和所有的帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

由于没有其他人回答,我会的。正如Aritra所说,我完全忘记了每次运行migrate命令时创建的迁移文件。我从模型中删除了这些模型字段,每当我再次迁移时,删除迁移文件中的文件时出错。手动进入并删除文件后,我的错误得到了修复。

数据库与django在其文件中的含义完全不同步。