django - OperationalError at ...没有这样的列:funcionarios_funcionario.user_id
问题描述
我是 Django 的新手,我遇到了一个问题。我在控制台中收到此消息:
django.db.utils.IntegrityError: The row in table 'funcionarios_funcionario' wi
th primary key '1' has an invalid foreign key: funcionarios_funcionario.empres
a_id contains a value '1' that does not have a corresponding value in empresa_
empresa.id.
当我运行应用程序时,我收到了这条消息
OperationalError at /admin/documentos/documento/add/
no such column: funcionarios_funcionario.user_id
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/documentos/documento/add/
Django Version: 3.1.2
Exception Type: OperationalError
Exception Value:
no such column: funcionarios_funcionario.user_id
课堂文件
from django.db import models
from apps.funcionarios.models import funcionario
# Create your models here.
class Documento(models.Model):
descricao = models.CharField(max_length=100)
pertence = models.ForeignKey(funcionario, on_delete=models.PROTECT)
def __str__(self):
return self.descricao
类函数
from django.db import models
from django.contrib.auth.models import User
from apps.departamentos.models import Departamento
from apps.empresa.models import Empresa
class funcionario(models.Model):
nome = models.CharField(max_length=100)
user = models.OneToOneField(User, on_delete=models.PROTECT)
departamentos = models.ManyToManyField(Departamento)
empresa = models.ForeignKey(Empresa, on_delete=models.PROTECT)
def __str__(self):
return self.nome
我该如何解决这个问题?
非常感谢!
解决方案
似乎您以某种方式删除了相关的 empresa,现在您有一个 IntegrityError,因为您在 ForeignKeys 中设置了“on_delete=models.PROTECT”。
Espero ter ajudado!
推荐阅读
- msbuild - 旧项目格式忽略 PackageReference 条件
- sql - 将 wkt 转换为 oracle 几何
- java - Kafka 主题不断添加到 DLT 主题中
- python - 尽管存在对象,但“NoneType”对象没有属性“seventh_five”
- javascript - 如何压缩 ObjectID 以使其更短
- r - 修改递归的 discogs 列表
- python - 升级到 Chromedriver 76 导致警报自动关闭
- python - 逐行计算熊猫数据帧
- c# - SSIS 包在我的机器上执行良好,但作为 SQL 作业失败
- python - 如何修复' raise ValueError("unknown url type: %r" % self.full_url) ValueError: unknown url type: '''