出现OperationalError,原因为...没有此类列:funcionarios_funcionario.user_id

时间:2020-10-10 23:59:34

标签: django django-models

我是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

我该如何解决?

非常感谢您!

1 个答案:

答案 0 :(得分:1)

由于您在外键中设置了“ on_delete = models.PROTECT”,因此您似乎以某种方式删除了相关的Empresa,但现在出现了IntegrityError。

Espero ter ajudado!