我在Django中有这个模型:
class Unit(models.Model):
Name = models.CharField(max_length=512)
type = models.CharField(max_length=512)
Hull_Number = models.CharField(max_length=100)
email = models.CharField(max_length=512)
Commander = models.ForeignKey(User)
ParentUnit = models.ForeignKey('Unit', blank=True)
Address1 = models.CharField(max_length=512)
Address2 = models.CharField(max_length=512, blank=True)
City = models.CharField(max_length=255)
Province = models.CharField(max_length=255)
Country = models.CharField(max_length=255)
PostalCode = models.CharField(max_length=10)
Avatar = models.FileField(upload_to='UnitAvatar/%Y/%m/%d', blank=True)
Cover = models.FileField(upload_to='UnitCover/%Y/%m/%d', blank=True)
Facebook = models.URLField(default='', blank=True)
GooglePlus = models.URLField(default='', blank=True)
Twitter = models.URLField(default='', blank=True)
Website = models.URLField(default='', blank=True)
第六个字段称为ParentUnit。它是一个自我引用的父/子引用。我的问题是第一个记录。
在尝试插入第一条记录时,我得到的回应是:
null value in column "ParentUnit_id" violates not-null constraint
DETAIL: Failing row contains (3, USS Enterprise, Heavy Cruiser, NCC-1701, arcee123@gmail.com, 123 Anywhere Street, , Anytown, AnyProvince, USA, 12345, , , , , , , 1, null).
因为ParentUnit_id是FK,所以当migrate
出现时,它会期待非空响应。如果是这种情况,如何添加没有父记录的子项?
感谢。