python - 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
解决方案
推荐阅读
- spring-boot - Liquibase Gradle 插件的 url 和 referenceUrl 似乎颠倒了
- react-native - 安全密码字段在发布时不受保护
- c++ - 定义子类构造函数时使用花括号调用基类构造函数?
- c# - 将预制件作为游戏对象的子对象统一生成
- directshow - GMFBridge DirectShow 滤镜 SetLiveTiming 效果
- java - 覆盖功能区服务器列表以从领事获取主机名列表
- javascript - Firebase 云消息传递每个 then() 都应该返回一个值或抛出 promise/always-return
- python-3.x - 如果响应条件,boto3 无法执行
- python - 用大图像重新训练 MobileNet
- java - 为什么 Espresso 不等待实际观看条件?