insert or update on table "Users" violates foreign key constraint "Users_LocalUnit_fkey"

66 Views Asked by At

I have to tables: Users and LocalUnits. I created a foreign key of localunit in Users, did a migration, everything ok.

When I tried to update the database I got this error:

23503: insert or update on table "Users" violates foreign key constraint "Users_LocalUnit_fkey"

Here is my code:

entity.Property(e => e.IdLocalUnit)
    .HasColumnName("local_unit_id");

entity.HasOne(e=>e.IdLocalUnitNavigation)
    .WithMany(p => p.Users)
    .HasForeignKey(e => e.IdLocalUnit)
    .OnDelete(DeleteBehavior.ClientSetNull)
    .HasConstraintName("Users_LocalUnit_fkey")

I must tell you that the local_unit_id column doesn't yet exist in the User table. I tried to apply a migration of the User's table in order to create the local_unit_id column but I couldn't, I got this error:

42701: column "is_registered_office" of relation "LocalUnits" already exists

which is true and I do not know how to do.

0

There are 0 best solutions below