Django-DB-Migrations: cannot ALTER TABLE because it has pending trigger events
I want to remove null=True from a TextField: - footer=models.TextField(null=True, blank=True) + footer=models.TextField(blank=True, default='') I created a schema migration: manage.py schemamigration fooapp --auto Since some footer columns contain NULL I get this error if I run the migration: django.db.utils.IntegrityError: column "footer" contains null values I added this to the schema migration: for sender in orm['fooapp.EmailSender'].objects.filter(footer=None): sender.footer='' sender.save() Now I get: django.db.utils.DatabaseError: cannot ALTER TABLE "fooapp_emailsender" because it has pending trigger events What is wrong?
Another reason for this maybe because you try to set a column to
NOT NULL when it actually already has
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.
Have just hit this problem. You can also use db.start_transaction() and db.commit_transaction() in the schema migration to separate data changes from schema changes. Probably not so clean as to have a separate data migration but in my case I would need schema, data, and then another schema migration so I decided to do it all at once.
- Database Administration Tutorials
- Programming Tutorials & IT News
- Linux & DevOps World
- Entertainment & General News
- Games & eSport