title: How to properly make migrations when adding a new unique field
body: <p>I added a new field to one of my models:</p>
  
  <pre><code>class Agency(models.Model):
      email = models.EmailField(unique=True, verbose_name=_("e-mail"))
  </code></pre>
  
  <p>As this field cannot be blank, <code>django-admin makemigrations</code> requested me to provide one-off default, which I did. Here is the generated migration:</p>
  
  <pre><code># Generated by Django 1.9.4 on 2016-03-20 10:38
  from __future__ import unicode_literals
  
  from django.db import migrations, models
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('accounts', '0008_auto_20160226_1226'),
      ]
  
      operations = [
          migrations.AddField(
              model_name='agency',
              name='email',
              field=models.EmailField(default='example@example.fr', max_length=254, unique=True, verbose_name='e-mail'),
              preserve_default=False,
          ),
      ]
  </code></pre>
  
  <p>As expected, <code>django-admin migrate</code> throwed an error:</p>
  
  <pre><code>psycopg2.IntegrityError: could not create unique index "accounts_agency_email_key"
  DETAIL:  Key (email)=(example@example.fr) is duplicate.
  </code></pre>
  
  <p>I thought I could edit the migration to set unique values before making the field unique. So I tried:</p>
  
  <pre><code># -*- coding: utf-8 -*-
  # Generated by Django 1.9.4 on 2016-03-20 10:38
  from __future__ import unicode_literals
  
  from django.db import migrations, models
  from django.utils.text import slugify
  
  
  def set_email(apps, schema_editor):
      Agency = apps.get_model('accounts', 'Agency')
      for agency in Agency.objects.all():
          agency.email = '{}@example.fr'.format(slugify(agency.name))
          agency.save()
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('accounts', '0008_auto_20160226_1226'),
      ]
  
      operations = [
          migrations.AddField(
              model_name='agency',
              name='email',
              field=models.EmailField(default='', max_length=254, blank=True, verbose_name='e-mail'),
              preserve_default=False,
          ),
          migrations.RunPython(set_email),
          migrations.AlterField(
              model_name='agency',
              name='email',
              field=models.EmailField(max_length=254, unique=True, verbose_name='e-mail'),
              preserve_default=False,
          ),
      ]
  </code></pre>
  
  <p>Unfortunately I get this error when running <code>django-admin migrate</code>:</p>
  
  <pre><code>django.db.utils.OperationalError: cannot ALTER TABLE "accounts_agency" because it has pending trigger events
  </code></pre>
  
  <p>My guess is that <code>operations</code> are not executed synchronously.</p>
  
  <p>I think I could fix the issue by splitting the migration into two migrations, but I'd like to know if I can do it in only one migration. What is the common way to create migrations when adding a new unique field in a model?</p>
  
  <p>PS: I also tried to use an F expression as default (<code>default=models.F('name') + '@example.fr'</code>) but it failed:</p>
  
  <pre><code>django.db.utils.IntegrityError: could not create unique index "accounts_agency_email_key"
  DETAIL:  Key (email)=(F(name) + Vallu(@example.fr)) is duplicated.
  </code></pre>