我有一个模型,它包含许多其他模型的ForeignKeys,出于某种原因,我发现我可以将其中一个模型保存得很好,但另一个模型我收到一条错误消息,说:IntegrityError: NOT NULL constraint failed: app_eofsr.flight_id
请参阅下面的我的模型.py:
# models.py
class Aircraft(models.Model):
esn = models.IntegerField()
acid = models.CharField(max_length=8)
engpos = models.IntegerField()
operator = models.CharField(max_length=5, default='---')
fleet = models.ForeignKey(Fleet, blank=True)
slug = models.SlugField(default=None, editable=False, max_length=50, unique=True)
class EoFSR(models.Model):
datetime_eofsr = models.DateTimeField(default=None)
flight_number = models.CharField(max_length=10)
city_from = models.CharField(max_length=4)
city_to = models.CharField(max_length=4)
这两者都被输入到这个模型中:
# models.py
class Flight(models.Model):
flight_number = models.CharField(max_length=10)
city_from = models.CharField(max_length=4, default=None)
city_to = models.CharField(max_length=4, default=None)
datetime_start = models.DateTimeField(default=None)
aircraft = models.ForeignKey(Aircraft, on_delete=models.CASCADE)
eofsr = models.ForeignKey(EoFSR, on_delete=models.CASCADE)
奇怪的是,我可以毫无问题地保存飞机记录,但不能保存EoFSR记录并接收NOT NULL约束错误消息。我已经完成了通常的迁移删除,甚至尝试删除db.sqlite3,但仍然没有成功!有什么建议吗?
尝试在模型中设置null=True您有ForeignKey((,如:
aircraft = models.ForeignKey(Aircraft, null=True, on_delete=models.CASCADE)
,然后进行迁移并迁移