Home > Back-end >  Could not create unique index "" when I use string PK
Could not create unique index "" when I use string PK

Time:07-05

I have the following entities:

public class File
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public string Name { get; set; }
}

and

public class FamilyTree
{
    public int Id { get; set; }
    public string FileName { get; set; }
    [ForeignKey(nameof(FileName))]
    public File File { get; set; }
    public string Title { get; set; }
 }

My ApplicationDbContext.cs has the following configuration:

...
        modelBuilder
            .Entity<FamilyTree>() 
            .HasOne(x => x.File)
            .WithOne()
            .HasForeignKey<FamilyTree>(x => x.FileName)
            .OnDelete(DeleteBehavior.Cascade);
...

Now I'm trying to make a migration and apply:

$ dotnet ef migrations add
$ dotnet ef database update

But I see the following error:

  Exception data:
    Severity: ERROR
    SqlState: 23505
    MessageText: could not create unique index "IX_FamilyTrees_FileName"
    Detail: Detail redacted as it may contain sensitive data. Specify 'Include Error Detail' in the connection string to include this information.
    SchemaName: public
    TableName: FamilyTrees
    ConstraintName: IX_FamilyTrees_FileName
    File: tuplesort.c
    Line: 4295
    Routine: comparetup_index_btree
23505: could not create unique index "IX_FamilyTrees_FileName"

What is wrong? I use .NET 6 and postgres

CodePudding user response:

It seems I just had data in the FamilyTrees table that interfered me to perform the migration somehow. What a pity that I did not receive an appropriate message.

  • Related