首页 > 解决方案 > Django 完整性错误:不存在的列中的空值

问题描述

我正在努力在 Django 中的两个模型(Experience & Scheduler)之间添加外键关系。当我最初将Experience的外键添加到Scheduler时,我最终收到了一个完整性错误。然后我删除了外键关系并相应地更新了我的模型,以便我可以测试。当我去保存我的 Scheduler 对象时,这仍然导致了一个错误(我不 100% 记得,但我很确定它仍然是一个 IntegrityError)。

我研究了一些不同的解决方案,我决定运行python manage.py flush以完全清除数据库,因为我在我的本地主机和我自己的分支上。即使在擦除数据库并删除外键关系后完整性错误仍然存​​在,我不知道为什么。

我一直试图弄清楚失败的行代表什么,因为它看起来不像任何一个模型。我发现的唯一一件事是,当我刷新错误页面时,列的索引 0 会增加 1。

完整性错误

体验模型:

from django.db import models

class Experience(models.Model):
    type = models.CharField(max_length = 10, null=False, blank=True)
    description = models.TextField(max_length=100, null=False, blank=True)
    image = models.ImageField(upload_to='uploads/experience/', blank=True)
    time = models.DateTimeField(null=False, blank=True)
    cost = models.DecimalField(max_digits=6, decimal_places=2, null=True)

调度器模型:

from django.db import models
from user.models import UserProfile
from museum.models import Simulator, Museum
from schedule.models import Event
from experience.models import Experience

# Create your models here:
class ScheduleEvent(Event):
    pilot = models.ForeignKey(
        UserProfile, on_delete=models.CASCADE, name="pilot", null=True, blank=True, related_name="pilot_event",
    )
    copilot = models.ForeignKey(
        UserProfile, on_delete=models.CASCADE, name="copilot", null=True, blank=True, related_name="copilot_event",
    )
    # experience = models.ForeignKey(Experience, on_delete=models.CASCADE, name="experience", null=True, blank=True)
    simulator = models.ForeignKey(Simulator, on_delete=models.CASCADE)
    is_claimed = models.BooleanField(verbose_name="Event is claimed and confirmed", name="is_claimed", default=False)

    def is_confirmed(self):
        #TODO Add logic to check confirmation of payment
        return True

标签: pythondjangodjango-models

解决方案


推荐阅读