南:不能ALTER TABLE因为它有待处理的触发事件 [英] south: cannot ALTER TABLE because it has pending trigger events

查看:331
本文介绍了南:不能ALTER TABLE因为它有待处理的触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从TextField中删除null = True:

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

由于某些页脚列包含NULL,如果我运行迁移,我会收到此错误:

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()

现在我得到:

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

有什么问题?

推荐答案

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

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()

然后创建模式迁移:

manage.py schemamigration fooapp --auto

现在您有两个交易,两步迁移应该可以运行。

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

这篇关于南:不能ALTER TABLE因为它有待处理的触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆