Python Forum
Need your help to fix my database relationship in Django model
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need your help to fix my database relationship in Django model
#1
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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Django: View is unable to find attributes of database model pythonpaul32 0 484 Dec-07-2023, 06:38 PM
Last Post: pythonpaul32
  Querying Django model db - from Jose Portilla’s Udemy course Drone4four 2 1,658 Aug-09-2022, 09:25 AM
Last Post: Addweb
  Save JSON data to sqlite database on Django Quin 0 2,804 Mar-26-2022, 06:22 PM
Last Post: Quin
  Error updating one to many relationship in Flask/ SQLAlchemy atindra 0 3,300 Apr-15-2021, 10:29 PM
Last Post: atindra
Photo After using models.Model on my class Im getting 'ImproperlyConfigured' error Django khavro 1 2,150 Apr-05-2021, 03:11 PM
Last Post: SheeppOSU
  [Django]calculate value in model smabubakkar 0 1,628 Apr-15-2020, 03:40 PM
Last Post: smabubakkar
  Flask-SqlAlchemy - Relationship failed to locate a name - Please Help howardrwb 0 5,148 Jan-31-2020, 08:38 PM
Last Post: howardrwb
  Send email to gmail after user fill up contact form and getting django database updat Man_from_India 0 2,071 Jan-22-2020, 03:59 PM
Last Post: Man_from_India
  how retrieve database save multiple data in web Django 2.1 taomihiranga 0 2,764 Jul-30-2019, 04:58 PM
Last Post: taomihiranga
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,246 Jun-30-2019, 12:21 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020