Migration of unique value isn't working
Question: I'm trying to execute a migration where it add a `UUID` field to an existing
table. I made the step by step of what is explained in the Django
documentation (<https://docs.djangoproject.com/en/1.8/howto/writing-
migrations/#migrations-that-add-unique-fields>), but it's not working for me.

I'm working with version 1.8.

The model is like this:

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

And I've 3 migrations file. Migration 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),
            ),
    ]
    

Migration 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),
           ),
        ]
    

Migration 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),
        ]
    

Bellow the error:

    
    
    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
    


Answer: In migration #3 you need to do `row.save()`. You may also need to do migration
#2 AFTER migration #3 as it changes the column to disallow nulls.

