我有两个模型,Product
和ProductImage
。当我尝试删除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如何处理级联删除?
答案 0 :(得分:0)
您显然只是将delete_flag = models.BooleanField(default=False)
的对象标记为已删除(ProductImage
),同时使用Product
的实际删除来打破ProductImage -> Product
外键约束。
要使其有效,您必须按照与Product
相同的方式实施ProductImage
删除。