Foreign key for selected character slots.

This commit is contained in:
Pieter-Jan Briers
2020-10-07 10:35:38 +02:00
parent 0532811744
commit 20eac0de84
3 changed files with 25 additions and 6 deletions

View File

@@ -20,10 +20,4 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
</ItemGroup>
<ItemGroup>
<Folder Include="Migrations\Postgres" />
<Folder Include="Migrations\Sqlite" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
namespace Content.Server.Database.Migrations.Postgres
{
[DbContext(typeof(PostgresServerDbContext))]
[Migration("20201006223000_SelectedCharacterSlotFk")]
public class SelectedCharacterSlotFk : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"ALTER TABLE preference
ADD CONSTRAINT ""FK_preference_profile_selected_character_slot_preference_id""
FOREIGN KEY (selected_character_slot, preference_id)
REFERENCES profile (slot, preference_id)
DEFERRABLE INITIALLY DEFERRED;");
}
}
}

View File

@@ -55,6 +55,12 @@ namespace Content.Server.Database
[Table("preference")]
public class Preference
{
// NOTE: on postgres there SHOULD be an FK ensuring that the selected character slot always exists.
// I had to use a migration to implement it and as a result its creation is a finicky mess.
// Because if I let EFCore know about it it would explode on a circular reference.
// Also it has to be DEFERRABLE INITIALLY DEFERRED so that insertion of new preferences works.
// Also I couldn't figure out how to create it on SQLite.
[Column("preference_id")] public int Id { get; set; }
[Column("user_id")] public Guid UserId { get; set; }
[Column("selected_character_slot")] public int SelectedCharacterSlot { get; set; }