Django-DB-Migrations:无法更改表,因为它具有未决的触发事件 [英] Django-DB-Migrations: cannot ALTER TABLE because it has pending trigger events

查看:208
本文介绍了Django-DB-Migrations:无法更改表,因为它具有未决的触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文本字段中删除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:页脚列包含空值

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.

这篇关于Django-DB-Migrations:无法更改表,因为它具有未决的触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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