django on_delete 级联删除不生效么?
有个疑惑, django 设置 on_delete =models.CASCADE 好像没作用
class User(models.Model): email = models.CharField(max_length=100, unique=True) password = models.CharField(max_length=100) create_time = models.DateTimeField(auto_now_add=True)
class Session(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) token = models.CharField(max_length=100, unique=True)
数据库是 mysql ,默认引擎 innodb, migrate 之后并没有看到对应的表有设置 ON DELETE 属性 而且我如果先删除主表会提示 error ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails 另外, django 也不支持 on_update ?
我试着进数据库手动添加了 ON DELETE CASCADE ,是可以的 那么, models 的这个 on_delete 属性有什么意义呢。
----------------------- 以下是精选回复-----------------------
答:连文档都不读吗?
Cascade deletes. Django emulates the behavior of the SQL constraint ON DELETE CASCADE and also deletes the object containing the ForeignKey
0条评论