title: Django migrations using RunPython to commit changes
body: <p>I want to alter a foreign key in one of my models that can currently have NULL values to not be nullable.</p>
  
  <p>I removed the <code>null=True</code> from my field and ran <code>makemigrations</code></p>
  
  <p>Because I'm an altering a table that already has rows which contain NULL values in that field I am asked to provide a one-off value right away or edit the migration file and add a <code>RunPython</code> operation.</p>
  
  <p>My RunPython operation is listed BEFORE the <code>AlterField</code> operation and does the required update for this field so it doesn't contain NULL values (only rows who already contain a NULL value).</p>
  
  <p>But, the migration still fails with this error:
  <code>django.db.utils.OperationalError: cannot ALTER TABLE "my_app_site" because it has pending trigger events</code></p>
  
  <p>Here's my code:</p>
  
  <pre><code># -*- coding: utf-8 -*-
  from __future__ import unicode_literals
  
  from django.db import models, migrations
  
  def add_default_template(apps, schema_editor):
      Template = apps.get_model("my_app", "Template")
      Site = apps.get_model("my_app", "Site")
  
      accept_reject_template = Template.objects.get(name="Accept/Reject")
      Site.objects.filter(template=None).update(template=accept_reject_template)    
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('my_app', '0021_auto_20150210_1008'),
      ]
  
      operations = [
          migrations.RunPython(add_default_template),
          migrations.AlterField(
              model_name='site',
              name='template',
              field=models.ForeignKey(to='my_app.Template'),
              preserve_default=False,
          ),
      ]
  </code></pre>
  
  <p>If I understand correctly this error may occur when a field is altered to be not-nullable but the field contains null values.
  In that case, the only reason I can think of why this happens is because the <code>RunPython</code> operation transaction didn't "commit" the changes in the database before running the <code>AlterField</code>.</p>
  
  <p>If this is indeed the reason - how can I make sure the changes reflect in the database?
  If not - what can be the reason for the error?</p>
  
  <p>Thanks!</p>