T.Pool
T.Pool

Reputation: 155

Django Admin Panel can not add any record to a table

In my Django admin panel, whenever i want to add some records in a table i get this error :

IntegrityError at /admin/pool/orders/add/

(1452, 'Cannot add or update a child row: a foreign key constraint fails (projectname.orders, CONSTRAINT orders_ibfk_3 FOREIGN KEY (user_phone) REFERENCES users (phone) ON DELETE NO ACTION ON UPDATE NO ACTION)')

Here are my Models :

class Orders(models.Model):
    objects = jmodels.jManager()
    product = models.ForeignKey('Products', models.DO_NOTHING)
    user_phone = models.ForeignKey('Users', models.DO_NOTHING, db_column='user_phone')
    order_date = jmodels.jDateField()
    status = models.CharField(max_length=11)
    price = models.CharField(max_length=11)
    count = models.IntegerField()

    class Meta:
        managed = False
        db_table = 'Orders'
        verbose_name_plural = "user orders"

    def __str__(self):
        return '%s------- (%s)' % (self.user_phone,self.status)


class Users(models.Model):
    objects = jmodels.jManager()
    phone = models.CharField(unique=True, max_length=11)
    name = models.CharField(max_length=80)
    family = models.CharField(max_length=80)
    nationalcode = models.CharField(max_length=11, blank=True, null=True)
    city = models.CharField(max_length=80, blank=True, null=True)
    address = models.TextField(blank=True, null=True)
    profilepic = models.TextField(blank=True, null=True)
    profiletext = models.TextField(blank=True, null=True)
    bons = models.CharField(max_length=11, blank=True, null=True)
    charge = models.CharField(max_length=11, blank=True, null=True)
    registerdate = jmodels.jDateField()
    status = models.CharField(max_length=11, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'Users'
        verbose_name_plural = "Users"

    def __str__(self):
        return '%s %s ---- (%s)' % (self.name,self.family,self.phone)

Additional: Along with this I have another issue. It is that I have 150 records in orders table, but I can't see them in Django admin panel, instead it just displays the count.

Upvotes: 1

Views: 1904

Answers (1)

Sanip
Sanip

Reputation: 1810

You error can be caused due to mismatch in STORAGE_ENGIENE. I think there are some tables of INNODB and some tables of MyISAM. Try converting them into a consistent type.

Further if this is not the error, then it is being caused due to forward references in your database. If this is the cause, then you can turn off the foreign key checks by editing your Django settings as:

DATABASES = {
    'default': {
                ...         
               'OPTIONS': {
                         "init_command": "SET foreign_key_checks = 0;",
                },
       }
}

For more details, you can refer this post: Django "Cannot add or update a child row: a foreign key constraint fails"

EDIT:

To get the value of phone in your user_phone field, you need to use ForeignKey.to_field in your Orders model as:

user_phone = models.ForeignKey('Users', to_field='phone')

This is required because Django uses the primary key of the User model to store the value in user_phone of Orders model. Using the above mentioned script helps to get the value of the field you desire in the foreign key field.

While doing so, you need to ensure that phone field of the User model is has unique=True set.

Check this link for official Django documentation to_field

Upvotes: 1

Related Questions