移植独特的价值并不奏效

时间:2015-11-21 17:18:48

标签: python django django-models migration

我尝试执行迁移,将UUID字段添加到现有表中。我按照Django文档(https://docs.djangoproject.com/en/1.8/howto/writing-migrations/#migrations-that-add-unique-fields)中的说明逐步完成,但它不适用于我。

我正在使用1.8版本。

模型是这样的:

class Event(models.Model):
    key_public = models.UUIDField(default=uuid.uuid4, unique=True)
    key_private = models.UUIDField(default=uuid.uuid4, unique=True)

我有3个迁移文件。 迁移1:

from __future__ import unicode_literals

from django.db import models, migrations
import uuid

class Migration(migrations.Migration):

    dependencies = [
        ('events', '0007_auto_20151008_1313'),
    ]

    operations = [
        migrations.AddField(
            model_name='Event',
            name='key_private',
            field=models.UUIDField(null=True, default=uuid.uuid4),
        ),

        migrations.AddField(
            model_name='Event',
            name='key_public',
            field=models.UUIDField(null=True, default=uuid.uuid4),
        ),
]

迁移2:

from __future__ import unicode_literals

from django.db import models, migrations
import uuid


class Migration(migrations.Migration):

    dependencies = [
        ('events', '0008_auto_20151121_1642'),
    ]

    operations = [
        migrations.AlterField(
            model_name='Event',
            name='key_private',
            field=models.UUIDField(default=uuid.uuid4, unique=True),
       ),

       migrations.AlterField(
           model_name='Event',
           name='key_public',
           field=models.UUIDField(default=uuid.uuid4, unique=True),
       ),
    ]

迁移3:

from __future__ import unicode_literals

from django.db import models, migrations
import uuid

def gen_uuid(apps, schema_editor):
    Event = apps.get_model('events', 'Event')

    for row in Event.objects.all():
        row.key_private = uuid.uuid4()
        row.key_public = uuid.uuid4()

class Migration(migrations.Migration):
    dependencies = [
        ('events', '0009_auto_20151121_1644'),
    ]

    operations = [
        migrations.RunPython(gen_uuid, reverse_code=migrations.RunPython.noop),
    ]

吼错误:

Traceback (most recent call last):
   File "event/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
   File "/Users/evandrolg/.virtualenvs/event/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
   File "/Users/evandrolg/.virtualenvs/event/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
   File "/Users/evandrolg/.virtualenvs/event/lib/python3.4/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
   File "/Users/evandrolg/.virtualenvs/event/lib/python3.4/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
  File "/Users/evandrolg/.virtualenvs/event/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 "/Users/evandrolg/.virtualenvs/event/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 "/Users/evandrolg/.virtualenvs/event/lib/python3.4/site-packages/django/db/migrations/executor.py", line 147, in apply_migration
state = migration.apply(state, schema_editor)
 File "/Users/evandrolg/.virtualenvs/event/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 "/Users/evandrolg/.virtualenvs/event/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 "/Users/evandrolg/.virtualenvs/event/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 484, in alter_field
old_db_params, new_db_params, strict)
 File "/Users/evandrolg/.virtualenvs/event/lib/python3.4/site-packages/django/db/backends/sqlite3/schema.py", line 203, in _alter_field
self._remake_table(model, alter_fields=[(old_field, new_field)])
 File "/Users/evandrolg/.virtualenvs/event/lib/python3.4/site-packages/django/db/backends/sqlite3/schema.py", line 147, in _remake_table
self.quote_name(model._meta.db_table),
 File "/Users/evandrolg/.virtualenvs/event/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 111, in execute
cursor.execute(sql, params)
 File "/Users/evandrolg/.virtualenvs/event/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
 File "/Users/evandrolg/.virtualenvs/event/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
 File "/Users/evandrolg/.virtualenvs/event/lib/python3.4/site-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
 File "/Users/evandrolg/.virtualenvs/event/lib/python3.4/site-packages/django/utils/six.py", line 658, in reraise
raise value.with_traceback(tb)
 File "/Users/evandrolg/.virtualenvs/event/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
 File "/Users/evandrolg/.virtualenvs/event/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py", line 318, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed: events_event__new.key_private

1 个答案:

答案 0 :(得分:0)

在迁移#3中,您需要执行row.save()。您可能还需要在迁移#3之后执行迁移#2,因为它会将列更改为禁止空值。