Python Forum

Full Version: Need your help to fix my database relationship in Django model
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

I have created a simple database admin panel (Django2.0.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-
Error:
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)
    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()
    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)
    class Meta:
        managed = False
        db_table = 'merchant'

    def __str__(self):
        return self.name
class Feed(models.Model):
    merchant = models.ForeignKey(Merchant, on_delete=models.CASCADE)
    merchant_store_id = models.CharField(max_length=100)
    merchant_product_id = models.CharField(max_length=100)

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