Django迁移陷入困境

时间:2015-07-26 17:46:41

标签: django postgresql django-models django-south

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

python manage.py makemigrations

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

我说

python manage.py migrate

我不太开心。向表kernel/migrations/0002_auto_20150707_1459.py添加字段date_of_birth的文件userprofile失败。即使我非常确定应用了迁移。因此,永远不会应用迁移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=#

3 个答案:

答案 0 :(得分:8)

对于社区,问题是django_migrations表未使用0002_auto_20150707_1459更新,即使迁移实际应用于table,如帖子所述。

解决方案是在django_migrations表格中插入一个新行,其中包含以下数据,以便跳过迁移0002

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

跳过迁移必须非常谨慎,因此请在跳过之前检查所有细节。

答案 1 :(得分:1)

对于发现此帖子的任何人:我只是帮助同事调试类似的问题(相同的错误消息),在这种情况下,问题的根源是尝试给DateField一个默认值u""(其中对于日期而言当然是无效的)而不是None

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

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

答案 2 :(得分:0)

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

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

对于每个 django 应用

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

※ about fake

这对我来说是一个解决方案