For effective work on the site, cookies and the processing of personal data are used. By using this site, you agree to the terms of use of the site. More

cannot alter table because it has pending trigger events

Aug. 9, 2016, 2:18 a.m.

You can see error cannot alter table because it has pending trigger events when property of your fields in database is cannot change in one transaction. To understand easy you can see examples.

Romeve blank=True in fields

So, there is model:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.utils.encoding import python_2_unicode_compatible

class MyModel(models.Model):
    title = models.CharField(max_length=100, null=True, blank=True)
    content = models.TextField(null=True, blank=True)
    template = models.CharField(max_length=255, null=True, blank=True)

    def __str__(self):
        return self.title

So null=True is extra useless property for CharField, TextField then you need remove null=True:

class MyModel(models.Model):
    title = models.CharField(max_length=100, blank=True)
    content = models.TextField(blank=True)
    template = models.CharField(max_length=255, blank=True)

Then run makemigrations. Note, if to run migrate now, then it gets error cannot alter table because it has pending trigger events. To fix this, write set_blank(apps, schema_editor) in created migration and added migrations.RunPython(set_blank) in operations list for running of our method:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

def set_blank(apps, schema_editor):
    fields = ('content', 'template', 'title')

    for obj in apps.get_model('my_app', 'MyModel').objects.all():
        old_field_values = {field: getattr(obj, field) for field in fields}

        for field in fields:
            setattr(obj, field, getattr(obj, field) or '')

        dirty = False
        for field, value in old_field_values.items():
            if value != getattr(obj, field):
                dirty = True
        if dirty:

def set_blank_simple(apps, schema_editor):
    MyModel = apps.get_model('my_app', 'MyModel')
    for obj in MyModel.objects.all():
        obj.content = obj.content or ''
        obj.template = obj.template or ''
        obj.title = obj.title or ''

class Migration(migrations.Migration):

    dependencies = [
        ('my_app', '0007_auto_20160509_1225'),

    operations = [
        # migrations.RunPython(set_blank_simple),  # simple function 
        migrations.RunPython(set_blank),  # more optimized function 
            field=models.TextField(default='', blank=True),
            field=models.CharField(default='', max_length=255, blank=True),
            field=models.CharField(default='', max_length=100, blank=True),

And error must be fixed. If error was not fixed then you need see to all changed fields more careful. May be, they are not ready to change in one transaction.

Rate this article

5 from 5 (total 2 ratings)

You can send feedback, suggestions or comments on this article using this form:

Fields marked by star ( * ) is required.

Thank you for yor feedback!

Article Author

The right to use this article on this page

Permission is granted to copy an article with its author and reference to the original without using the parameter rel="nofollow" in tag <a>. Usage:

Article Author: Maltsev Artem
Link to article: <a href=""></a>

More: Terms of site usage

Do you need to develop yourself or do you want to earn money?

Are you looking for good IT services or books? Save your time and look at my compilation of recommendations, which I use every day.
Go to recommendations

Comments: 0

You can leave a comment as an unregistered user. But by registering, you will be able to receive notifications of replies, and also have access to your personal account to view your comments.

To comment in one's own name you should log in or sign up in the usual way or through social networks:


There is no search on this site, so I offer to use usual search engine, for example, Google, adding "vivazzi" after your request.

Try it