The Python Oracle

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

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

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Track title: CC B Schuberts Piano Sonata No 16 D

--

Chapters
00:00 Question
01:00 Accepted answer (Score 187)
01:41 Answer 2 (Score 193)
01:57 Answer 3 (Score 29)
02:13 Answer 4 (Score 21)
04:08 Thank you

--

Full question
https://stackoverflow.com/questions/1283...

Answer 3 links:
[Django docs]: https://docs.djangoproject.com/en/dev/re...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #django #postgresql #djangomigrations



ANSWER 1

Score 210


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




ACCEPTED ANSWER

Score 202


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.




ANSWER 3

Score 41


At the operations I put SET CONSTRAINTS:

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



ANSWER 4

Score 11


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.