小编Hmo*_*ong的帖子

Postgres 不尊重 models.py 中 Django 的“on_delete = models.CASCADE”。为什么?

我刚刚从 SQLite3 迁移到 Postgres 12(使用 pgloader)。

我无法删除某些模型对象,因为还有其他模型对象引用它。

但是,我非常确定我的模型对象在引用模型对象中设置了“on_delete = models.CASCADE”(在 models.py 中的 Django 代码中)。

所以我得到了 Postgres 生成的错误:

...
django.db.utils.IntegrityError: update or delete on table "app_reply" violates foreign key constraint "app_replyimage_reply_id_fkey" on table "app_replyimage"
DETAIL:  Key (id)=(SRB6Nf98) is still referenced from table "app_replyimage".
Run Code Online (Sandbox Code Playgroud)

有没有办法(希望不用手动编辑 Postgres 中的表模式)来解决这个问题?

编辑:在下面添加一些代码...

模型.py

class ReplyImage(models.Model):
    id = models.CharField(default = make_id(), unique = True, primary_key = True, max_length = 8)
    
    file = models.ImageField(upload_to = customer_images_directory_path)
    
    reply = models.ForeignKey(Reply, on_delete = models.CASCADE, related_name = 'reply_images')
    
    #Meta
    created = …
Run Code Online (Sandbox Code Playgroud)

sqlite django postgresql django-models pgloader

3
推荐指数
1
解决办法
1562
查看次数

标签 统计

django ×1

django-models ×1

pgloader ×1

postgresql ×1

sqlite ×1