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

前端 未结 6 1365
梦毁少年i
梦毁少年i 2020-12-02 10:45

I want to remove null=True from a TextField:

-    footer=models.TextField(null=True, blank=True)
+    footer=models.TextField(blank=True, default=\'\')
         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 11:12

    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.

提交回复
热议问题