首页 > 解决方案 > 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

我该如何解决这个问题?

非常感谢!

标签: djangodjango-models

解决方案


似乎您以某种方式删除了相关的 empresa,现在您有一个 IntegrityError,因为您在 ForeignKeys 中设置了“on_delete=models.PROTECT”。

Espero ter ajudado!


推荐阅读