尽管on_delete被设置为Cascade,但Django相关的模型并没有被删除

时间:2015-10-12 12:45:06

标签: python django

我有两个模型,ProductProductImage。当我尝试删除Product时,尽管IntegrityError设置为级联,我仍然会从ProductImage表中提出on_delete

这是我的产品型号:

class Product(Tileable, models.Model):

    external_id = models.TextField(unique=True)
    merchant = models.ForeignKey(Merchant)
    deep_url = models.TextField()

    name = models.CharField(max_length=500)
    description = models.TextField()
    price = models.DecimalField(max_digits=19, decimal_places=2)
    colour = models.CharField(max_length=50, blank=True, null=True)
    date_added = models.DateTimeField(blank=True, default=timezone.now)

    category = models.ForeignKey(ProductCategory, related_name="products")
    brand = models.ForeignKey(Brand, related_name="products")
    tags = generic.GenericRelation("tags.Tagged", related_query_name='products')

    active = models.BooleanField(blank=True, default=False)

    class Meta:
        ordering = ('date_added',)  

这是我的ProductImage模型:

class ProductImage(models.Model):
    product = models.ForeignKey(Product, related_name="images", on_delete=models.CASCADE)

    image = ProcessedImageField(upload_to=product_image_upload_location,
                                format='JPEG',
                                options={'quality': 100},
                                processors=[ResizeToFit(550, 550)])

    image_thumb = ProcessedImageField(upload_to=product_thumb_upload_location,
                                      format='JPEG',
                                      options={'quality': 85},
                                      processors=[ResizeToFit(200, 200)])

当我打开shell并尝试删除产品时,我抛出了这个异常:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/sampeka/virtualenv/sharestyle_dev/lib/python3.4/site-packages/django/db/models/query.py", line 537, in delete
    collector.delete()
  File "/Users/sampeka/virtualenv/sharestyle_dev/lib/python3.4/site-packages/django/db/models/deletion.py", line 314, in delete
    sender=model, instance=obj, using=self.using
  File "/Users/sampeka/virtualenv/sharestyle_dev/lib/python3.4/site-packages/django/db/transaction.py", line 232, in __exit__
    connection.commit()
  File "/Users/sampeka/virtualenv/sharestyle_dev/lib/python3.4/site-packages/django/db/backends/base/base.py", line 173, in commit
    self._commit()
  File "/Users/sampeka/virtualenv/sharestyle_dev/lib/python3.4/site-packages/django/db/backends/base/base.py", line 142, in _commit
    return self.connection.commit()
  File "/Users/sampeka/virtualenv/sharestyle_dev/lib/python3.4/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/sampeka/virtualenv/sharestyle_dev/lib/python3.4/site-packages/django/utils/six.py", line 658, in reraise
    raise value.with_traceback(tb)
  File "/Users/sampeka/virtualenv/sharestyle_dev/lib/python3.4/site-packages/django/db/backends/base/base.py", line 142, in _commit
    return self.connection.commit()
django.db.utils.IntegrityError: insert or update on table "search_productimage" violates foreign key constraint "search_product_product_id_11456c99c3fb5b38_fk_search_product_id"
DETAIL:  Key (product_id)=(19) is not present in table "search_product".

我是否误解了Django如何处理级联删除?

1 个答案:

答案 0 :(得分:0)

您显然只是将delete_flag = models.BooleanField(default=False)的对象标记为已删除(ProductImage),同时使用Product的实际删除来打破ProductImage -> Product外键约束。

要使其有效,您必须按照与Product相同的方式实施ProductImage删除。