Django 迁移卡住了

标签 django postgresql django-models django-south

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

python manage.py makemigrations

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

我说

python manage.py migrate

我不那么开心了。将字段 date_of_birth 添加到表 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 表没有用 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')

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

关于Django 迁移卡住了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31640068/

相关文章:

django - 在 Django 表单中的两个字段中将一个字段设置为必填字段

MySQL:表与 View 与例程

postgresql - 无法从 postgresql 中声明的表中选择?

python - 如果在 Django 中输入任何无效数据,则在输入字段下方引发表单错误

Django迁移: Can't create table errno: 150

Django TEMPLATE_CONTEXT_PROCESSORS 被调用太多次

python - python django 的新闻来源

django - HTML5 离线存储 web 框架

postgresql - Rails 5 和 Postgres - 我可以在字段名称中使用 "?"吗?

python - 将不同格式的日期时间传递给 Django 中的 models.DateTimeField?