diff --git a/Content.Server.Database/Content.Server.Database.csproj b/Content.Server.Database/Content.Server.Database.csproj index 849374ed4b..4daa852b65 100644 --- a/Content.Server.Database/Content.Server.Database.csproj +++ b/Content.Server.Database/Content.Server.Database.csproj @@ -20,10 +20,4 @@ - - - - - - diff --git a/Content.Server.Database/Migrations/Postgres/20201006223000_SelectedCharacterSlotFk.cs b/Content.Server.Database/Migrations/Postgres/20201006223000_SelectedCharacterSlotFk.cs new file mode 100644 index 0000000000..0aecbee273 --- /dev/null +++ b/Content.Server.Database/Migrations/Postgres/20201006223000_SelectedCharacterSlotFk.cs @@ -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;"); + } + } +} diff --git a/Content.Server.Database/Model.cs b/Content.Server.Database/Model.cs index 341c05484b..5aa4f6ec81 100644 --- a/Content.Server.Database/Model.cs +++ b/Content.Server.Database/Model.cs @@ -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; }