Django-DB-Migrations: cannot ALTER TABLE because it has pending trigger events Django-DB-Migrations: cannot ALTER TABLE because it has pending trigger events python python

Django-DB-Migrations: cannot ALTER TABLE because it has pending trigger events


Another reason for this maybe because you try to set a column to NOT NULL when it actually already has NULL values.


Every migration is inside a transaction. In PostgreSQL you must not update the table and then alter the table schema in one transaction.

You need to split the data migration and the schema migration. First create the data migration with this code:

 for sender in orm['fooapp.EmailSender'].objects.filter(footer=None):    sender.footer=''    sender.save()

Then create the schema migration:

manage.py schemamigration fooapp --auto

Now you have two transactions and the migration in two steps should work.


At the operations I put SET CONSTRAINTS:

operations = [    migrations.RunSQL('SET CONSTRAINTS ALL IMMEDIATE;'),    migrations.RunPython(migration_func),    migrations.RunSQL('SET CONSTRAINTS ALL DEFERRED;'),]