Need your help to fix IntegrityError in my Django code.

13 views
Skip to first unread message

prateek gupta

unread,
Jul 2, 2018, 6:59:55 AM7/2/18
to Django users
Hi All,

I have created a simple database admin panel (Django2..6+Python3.6+Mysql5.7) where admin can operate create/edit/delete operations of database.
I am facing following IntegrityError  error when I try to Merge two products and click on Preview from my Django admin panel-
2018-07-02 16:17:56,428 [ERROR] exception.py:118 handle_uncaught_exception() : Internal Server Error: /where2buy/merchantproduct/
Traceback (most recent call last):
  File "C:\Users\prateek1.gupta\AppData\Local\Continuum\anaconda3\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\prateek1.gupta\AppData\Local\Continuum\anaconda3\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "C:\Users\prateek1.gupta\AppData\Local\Continuum\anaconda3\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "C:\Users\prateek1.gupta\AppData\Local\Continuum\anaconda3\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "C:\Users\prateek1.gupta\AppData\Local\Continuum\anaconda3\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
    res = self._query(query)
  File "C:\Users\prateek1.gupta\AppData\Local\Continuum\anaconda3\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "C:\Users\prateek1.gupta\AppData\Local\Continuum\anaconda3\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
    db.query(q)
  File "C:\Users\prateek1.gupta\AppData\Local\Continuum\anaconda3\lib\site-packages\MySQLdb\connections.py", line 277, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.IntegrityError: (1451, 'Cannot delete or update a parent row: a foreign key constraint fails (`productsearchv2`.`feed`, CONSTRAINT `feed_mproduct_id_fk` FOREIGN KEY (`merchant_id`, `merchant_product_id`) REFERENCES `merchant_product` (`merchant_id`, `merchant_product_id`) ON DELETE NO )')

I think issue is may be wrong relationship settings in my model file but couldn't able to find exact issue.
Can anyone please help me to find the issue? My code is as below-

class MerchantProduct(models.Model):
merchant = models.ForeignKey(Merchant, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.SET_NULL, blank=True, null=True)
merchant_product_id = models.CharField(max_length=100)
gtin = models.CharField(max_length=15, blank=True, null=True)
merchant_group_id = models.CharField(max_length=100, blank=True, null=True)
name = models.CharField(max_length=300)
description = models.TextField(blank=True, null=True)
image_urls = models.TextField(blank=True, null=True)
image_phashes = models.TextField(blank=True, null=True)
web_url = models.URLField(blank=True, null=True)
brand = models.CharField(max_length=100, blank=True, null=True)
category = models.CharField(max_length=150, blank=True, null=True)
color = models.CharField(max_length=100, blank=True, null=True)
size = models.CharField(max_length=100, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True, editable=False)
updated_at = models.DateTimeField(auto_now=True)

class Meta:
unique_together = (('merchant', 'merchant_product_id'),)
managed = False
db_table = 'merchant_product'

def __str__(self):
return self.name
class Product(models.Model):
merchants = models.ManyToManyField(Merchant, through='MerchantProduct')
brand = models.ForeignKey(Brand, related_name='brand', on_delete=models.SET_NULL, blank=True, null=True)
category = models.ForeignKey(Category, related_name='category', on_delete=models.SET_NULL, blank=True, null=True)
group = models.ForeignKey('self', related_name='ID', on_delete=models.SET_NULL, blank=True, null=True)
gtin = models.CharField(max_length=15, blank=True, null=True)
name = models.CharField(max_length=300)
description = models.TextField()
image_urls = models.TextField()
image_phashes = models.CharField(max_length=200, blank=True, null=True)
color = models.CharField(max_length=100, blank=True, null=True)
size = models.CharField(max_length=100, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True, editable=False)
updated_at = models.DateTimeField(auto_now=True)

class Meta:
managed = False
db_table = 'product'

def __str__(self):
return self.name

class Merchant(models.Model):
DATA_QUALITY_CHOICES = (
('approx', 'Approximate'),
('exact', 'Exact'),
)
name = models.CharField(unique=True, max_length=45)
display_name = models.CharField(unique=True, max_length=255)
url_name = models.CharField(unique=True, max_length=255)
data_quality = models.CharField(default='approx', max_length=6, choices=DATA_QUALITY_CHOICES)
status = models.CharField(default='I', max_length=1, choices=STATUS_CHOICES)
featured_rank = models.PositiveSmallIntegerField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True, editable=False)
updated_at = models.DateTimeField(auto_now=True)
config = models.TextField(default='{}', max_length=500, blank=True, null=True)

class Meta:
managed = False
db_table = 'merchant'

def __str__(self):
return self.name


Let me know if need any more info from side.
Reply all
Reply to author
Forward
0 new messages