南:不能ALTER TABLE,因为它有未决的触发事件

我想从TextField中删除null = True:

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

我创build了一个模式迁移:

 manage.py schemamigration fooapp --auto 

由于一些页脚列包含NULL我得到这个错误,如果我运行迁移:

 django.db.utils.IntegrityError: column "footer" contains null values 

我将这添加到模式迁移:

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

现在我得到:

 django.db.utils.DatabaseError: cannot ALTER TABLE "fooapp_emailsender" because it has pending trigger events 

哪里不对?

每个迁移都在一个事务中。 在PostgreSQL中,您不能更新表,然后在一个事务中更改表模式。

您需要拆分数据迁移和模式迁移。 首先用这个代码创build数据迁移:

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

然后创build模式迁移:

 manage.py schemamigration fooapp --auto 

现在你有两个交易,两个步骤的迁移应该工作。

另一个原因可能是因为你试图设置一个列为NOT NULL时,它实际上已经有NULL值。

刚刚遇到这个问题。 您还可以在模式迁移中使用db.start_transaction()和db.commit_transaction()将数据更改从模式更改中分离出来。 可能不是那么干净,以至于有一个单独的数据迁移,但在我的情况下,我将需要架构,数据,然后另一个模式迁移,所以我决定一次做这一切。