Integrity error

Hello, I’m having problems with the django admin, I’m trying to delete a table entry and I’m getting the following error:

Exception Type: IntegrityError
Exception Value:FOREIGN KEY constraint failed

These are the tables:

class Caso(models.Model):
    codigo_caso = models.CharField(max_length=180, null=False, blank=False, default="C000", verbose_name="Codigo Caso")
    nombre = models.CharField(max_length=180, null=True, blank=True, verbose_name="Nombre del Caso")
    tiempo = models.FloatField(null=True, blank=True, verbose_name="Tiempo en horas")
    dinero = models.FloatField(null=True, blank=True, verbose_name="Dinero disponible")
    fecha_inicio = models.DateTimeField(null=True, blank=True, verbose_name='Fecha de inicio')
    fecha_fin = models.DateTimeField(null=True, blank=True, verbose_name='Fecha de fin')
    fecha_creacion = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name='Fecha de creacion')
    fecha_actualizacion = models.DateTimeField(auto_now_add=True, null=True, blank=True,
                                               verbose_name='Fecha de actualizacion')
class EventoTemporal(models.Model):
    titulo = models.CharField(max_length=180, null=False, blank=False, verbose_name="Titulo")
    remitente = models.ForeignKey(Remitente, on_delete=models.CASCADE, null=True, blank=True, verbose_name="Remitente")
    cuerpo = models.TextField(null=True, blank=True, verbose_name="Cuerpo")
    documento = models.FileField(null=True, blank=True, verbose_name="Documento")
    multimedia = models.FileField(null=True, blank=True, verbose_name="Multimedia")
    tiempo = models.FloatField(null=True, blank=True, verbose_name="Horas desde el inicio del caso")
    id_caso = models.ForeignKey(Caso, on_delete=models.CASCADE, null=True, blank=False, verbose_name='Caso')
    id_pieza_informacion = models.ManyToManyField(PiezaInformacion, blank=False, verbose_name='Piezas Información')
    id_tareas = models.ManyToManyField(Tarea, blank=False, verbose_name='Tareas que desbloquea')
    fecha_creacion = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name='Fecha de creacion')
    fecha_actualizacion = models.DateTimeField(auto_now_add=True, null=True, blank=True,
                                               verbose_name='Fecha de actualizacion')

When I try to delete a Caso that has a EventoTemporal it throws the error, I think the error is because they are not deleted in the correct order because if I delete them manually in the correct order there is no problem.

Thanks in advance :slight_smile:

Any update on this?

I´m currently facing the same problem and I have no idea why is it happening?

There are multiple reasons why this could be happening.

  • Migrations not having been run

  • A database change that makemigrations didn’t pick up and create a migration for.

  • Database definitions and changes made outside the migration process

  • Having a foreign key in a relationship somewhere down the line with on_delete=models.PROTECT

If all the migrations are correct and up-to-date, and you’ve verified that you don’t have any ForeignKey fields preventing this from working, you’ll need to check the database itself to identify what constraints have been applied that need to be removed.

Thank you for your quick response.

I have made the checks you have pointed out and it still does not work, I have also tried to delete the migrations and create new ones and it does not work either. If the problem is due to the migrations, why creating them again does not solve it?

There can be definitions in the database that aren’t seen by the migration process.

You’re not going to be able to resolve this without examining the database schema directly.

Thanks for your answer again,
The problem is that I am not that familiarized with DDBB. Could you pleas briefly tell me what should I be looking for? Is there any tool that could help me?