Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dev] Potential fix to Grunts not being deleted #370

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

jannlemm0913
Copy link

TLDR

Fixes an error in Covenant's database schema that led to Grunts that could not be deleted anymore. I tested the fix a bit and have not seen any negative impact.

Explanation

Sometimes, I was unable to delete a Grunt from the list via the Action dropdown. The following error was shown in the console:

fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (1ms) [Parameters=[@p0='77' (DbType = String)], CommandType='Text', CommandTimeout='30']
      DELETE FROM "Folders"
      WHERE "Id" = @p0;
      SELECT changes();

I found that this was always the case when I used the file explorer in a Grunt. Looking at the database, two constraints caught my eye.
Table "FolderFileNodes":
CONSTRAINT "FK_FolderFileNodes_Folders_ParentId" FOREIGN KEY ("ParentId") REFERENCES "Folders" ("Id") ON DELETE RESTRICT,
Table "Folders":
CONSTRAINT "FK_Folders_Grunts_RootGruntId" FOREIGN KEY ("RootGruntId") REFERENCES "Grunts" ("Id") ON DELETE RESTRICT,
I modified CovenantContext.cs so that the constraints were set to cascade on deletion.
Line 108-124:

            builder.Entity<Grunt>()
                .HasMany(G => G.FolderRoots)
                //.WithOne(F => F.RootGrunt);
                .WithOne(F => F.RootGrunt)
                .OnDelete(DeleteBehavior.Cascade);
            
            builder.Entity<FolderFileNode>()
                .HasOne(N => N.Grunt)
                .WithMany(G => G.FolderFileNodes)
                .HasForeignKey(N => N.GruntId);

            builder.Entity<Folder>()
                .HasMany(F => F.Nodes)
                .WithOne()
                //.HasForeignKey(N => N.ParentId);
                .HasForeignKey(N => N.ParentId)
                .OnDelete(DeleteBehavior.Cascade);

I had to create a new covenant.db, since I could not modify the constraints on the existing database tables. After the change, I was able to use the file explorer in my Grunts and then also successfully delete them.

@jannlemm0913 jannlemm0913 changed the title Potential fix to Grunts not being deleted [dev] Potential fix to Grunts not being deleted Nov 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant