Django 迁移卡住了

Django migration stuck

我有一个新字段要添加到我的数据库中。所以我说

python manage.py makemigrations

正确创建 kernel/migrations/0003_auto_20150726_1911.py。我检查了内容,一切看起来都很好。

我说

python manage.py migrate

而且我不太开心。将字段 date_of_birth 添加到 table userprofile 的文件 kernel/migrations/0002_auto_20150707_1459.py 失败。即使我很确定应用了迁移。因此从未应用迁移 0003。

这是制作。 :( 我完全不确定该怎么做才能应用 0003 而不是软管 django。建议?

其余部分是支持文档:

迁移

╭╴ (master=) [virt]╶╮
╰ [T] django@beta13:migrations $ cat 0002_auto_20150707_1459.py 
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
    ('kernel', '0001_initial'),
]

operations = [
    migrations.AlterField(
    model_name='userprofile',
    name='date_of_birth',
    field=models.DateField(null=True, blank=True),
    ),
]
╭╴ (master=) [virt]╶╮
╰ [T] django@beta13:migrations $ cat 0003_auto_20150726_1911.py 
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings


class Migration(migrations.Migration):

dependencies = [
    migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ('kernel', '0002_auto_20150707_1459'),
]

operations = [
    migrations.AddField(
    model_name='trippending',
    name='requesting_user',
    field=models.ForeignKey(default=1, related_name='trippending_requesting', to=settings.AUTH_USER_MODEL),
    preserve_default=False,
    ),
    migrations.AddField(
    model_name='userprofile',
    name='can_see_pending_trips',
    field=models.BooleanField(default=False),
    ),
]
╭╴ (master=) [virt]╶╮
╰ [T] django@beta13:migrations $ 

错误

(该网站以法语运行,但我认为错误很明显。)

╭╴ (master %=) [virt]╶╮
╰ [T] django@beta13:django $ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages, admindocs
  Apply all migrations: admin, sessions, custom_user, auth, kernel, contenttypes, registration, sites
Synchronizing apps without migrations:
  Creating tables...
Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying kernel.0002_auto_20150707_1459...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
  File "/src/django/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
  File "/src/django/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/src/django/venv/lib/python3.4/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
  File "/src/django/venv/lib/python3.4/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
  File "/src/django/venv/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/src/django/venv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File "/src/django/venv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 147, in apply_migration
state = migration.apply(state, schema_editor)
  File "/src/django/venv/lib/python3.4/site-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/src/django/venv/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
  File "/src/django/venv/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 484, in alter_field
old_db_params, new_db_params, strict)
  File "/src/django/venv/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 571, in _alter_field
old_default = self.effective_default(old_field)
  File "/src/django/venv/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 211, in effective_default
default = field.get_db_prep_save(default, self.connection)
  File "/src/django/venv/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save
prepared=False)
  File "/src/django/venv/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1322, in get_db_prep_value
value = self.get_prep_value(value)
  File "/src/django/venv/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1317, in get_prep_value
return self.to_python(value)
  File "/src/django/venv/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1287, in to_python
params={'value': value},
django.core.exceptions.ValidationError: ["Le format de date de la valeur «\xa0\xa0» n'est pas valide. Le format correct est AAAA-MM-JJ."]
╭╴ (master %=) [virt]╶╮
╰ 1,[T] django@beta13:django $ 

数据

我检查了 postgres,希望能找到不对劲的地方。但一切看起来都很好。

mydb=# select date_of_birth from kernel_userprofile;
 date_of_birth 
---------------
 2018-10-23
 1972-05-31
 1978-10-21
 2008-12-29
 1967-08-26
 2015-07-26
(6 rows)

mydb=#

对于社区,问题是 django_migrations table 未更新为 0002_auto_20150707_1459,即使迁移实际上已在 table 上应用,如 [=] 中所述21=].

解决方案是在 django_migrations table 中插入一个新行,其中包含如下数据,因此跳过迁移 0002

INSERT INTO DJANGO_MGRATIONS ('app', 'name', 'applied') VALUES ('appname', '0002_auto_20150707_1459', '2015-07-07 00:00')

Skipping migration must be done with extreme caution, hence check all details before skipping.

对于发现此问题的任何人 post:我刚刚帮助同事调试了一个类似的问题(相同的错误消息),在这种情况下,问题的根源是试图为 DateField 提供默认值 u""(这当然对日期无效)而不是 None

实际上,有趣的部分是之前的迁移(创建模型)已经有这个错误的默认值,但仍然 运行 无缝,只有在尝试以任何方式更改字段时才会出现错误。

此修复需要编辑 previous 迁移以设置合理的默认值,因为 Django 使用先前迁移的模式描述来获取先前的默认值。

同样的问题发生了,但是没有任何日志

  1. 起初我通过 运行
  2. 确定哪个应用程序有问题
python manage.py migrate app_name

每个 django 个应用

  1. 之后我通过 运行
  2. 检查应用程序的迁移
python manage.py showmigrations app_name
  1. 并将第一个未完成的迁移标记为 运行
  2. 已完成
python manage.py migrate --fake app_name migration_name

※ about fake

这就是我的解决方案