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
, CONSTRAINTorders_ibfk_3
FOREIGN KEY (user_phone
) REFERENCESusers
(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.