【发布时间】:2021-11-19 13:46:19
【问题描述】:
我有 2 个模型 User 是 Django 的 auth_user,以及 HostCourse 与 User 有多对多关系
class HostCourse(TimeStamped, models.Model):
created_by = models.ManyToManyField(User, blank=True)
...
我正在使用 Postgresql,我正在尝试从 dbshell 中删除 HostCourse 中的数据。
delete from courses_hostcourse;
当我从 HostCourse 中删除一些数据时,它会抛出以下错误:
ERROR: update or delete on table "courses_hostcourse" violates foreign key constraint "courses_hostcourse_c_hostcourse_id_ec1070c3_fk_courses_h" on table "courses_hostcourse_created_by"
如果我将包含on_delete=models.CASCASE,是否也会删除相应HostCourse 的用户?如果是,我如何找到解决方法,以便删除课程而不是用户?
【问题讨论】:
标签: django postgresql django-models many-to-many