I want to change the foreign key in one of my models that currently cannot have nullable nulls.
I removed null=True from my field and ran makemigrations
Since I am modifying a table that already has rows that contain NULL values ββin this field, I am prompted to immediately provide a one-time value or edit the migration file and add the RunPython operation.
My RunPython operation is displayed before the AlterField operation and performs the necessary update for this field, so it does not contain NULL values ββ(only rows that already contain NULL).
But the migration still fails with this error: django.db.utils.OperationalError: cannot ALTER TABLE "my_app_site" because it has pending trigger events
Here is my 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, ), ]
If I understand correctly, this error can occur when the field is changed so that it is not null, but the field contains null values. In this case, the only reason I can understand why this is happening is because the transaction of the RunPython operation RunPython not β RunPython β the changes to the database before running AlterField .
If this is really the reason - how can I make sure the changes are reflected in the database? If not, what could be causing the error?
Thank!
django django-migrations
Gabriel Amram Feb 10 '15 at 10:57 2015-02-10 10:57
source share