一、问题: 用django生成自关联的表格后在添加数据的时候出现以下问题
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (test2
.areas
, CONSTRAINT areas_pid_id_534d967d_fk_areas_id
FOREIGN KEY (pid_id
) REFERENCES areas
(id
))
django 模型类代码:
from django.db import models
# Create your models here.
class Areas(models.Model):
"""创建省市县的数据库关联模型"""
# 区域名称
name = models.CharField(max_length=20, null=True, blank=True)
# 区域上级标识,外键
pid = models.ForeignKey('Areas', on_delete=models.CASCADE, null=True, blank=True)
# 地名简称
sname = models.CharField(max_length=40, null=True, blank=True)
# 地区等级
leval = models.CharField(max_length=11, null