From 05ec55a181bcf081bf140347d43db51ab9ea2a8f Mon Sep 17 00:00:00 2001 From: Brutus5000 Date: Mon, 15 Jun 2026 21:29:08 +0200 Subject: [PATCH 1/2] Add login.avatar_id FK for selected avatar Adds a nullable foreign key on the login table referencing avatars_list, making "currently selected avatar" a single authoritative value per user. The legacy avatars.selected flag is kept (and still writable) for backwards compatibility; the API mirrors both sides on writes and provides a reconciler to repair drift. Co-Authored-By: Claude Opus 4.7 (1M context) --- migrations/V143__login_avatar_fk.sql | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 migrations/V143__login_avatar_fk.sql diff --git a/migrations/V143__login_avatar_fk.sql b/migrations/V143__login_avatar_fk.sql new file mode 100644 index 00000000..ee34fae0 --- /dev/null +++ b/migrations/V143__login_avatar_fk.sql @@ -0,0 +1,15 @@ +-- Add a single "currently selected avatar" reference on the login table. +-- The legacy `avatars.selected` flag is kept (and still user-writable) for +-- backwards compatibility; the API mirrors both sides on writes and exposes a +-- reconciler to repair any drift. + +ALTER TABLE login + ADD COLUMN avatar_id mediumint(8) unsigned DEFAULT NULL, + ADD CONSTRAINT fk_login_avatar + FOREIGN KEY (avatar_id) REFERENCES avatars_list (id) + ON DELETE SET NULL ON UPDATE CASCADE; + +-- Backfill from the legacy selected flag. +UPDATE login l + JOIN avatars a ON a.idUser = l.id AND a.selected = 1 +SET l.avatar_id = a.idAvatar; From bf467f856925879afc9e75deae4f946131b3ee57 Mon Sep 17 00:00:00 2001 From: Brutus5000 Date: Mon, 15 Jun 2026 21:32:41 +0200 Subject: [PATCH 2/2] Fix avatar_id column type to match avatars_list.id MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit avatars_list.id is int(11) unsigned, not mediumint — the mismatch caused errno 150 "Foreign key constraint is incorrectly formed". Co-Authored-By: Claude Opus 4.7 (1M context) --- migrations/V143__login_avatar_fk.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migrations/V143__login_avatar_fk.sql b/migrations/V143__login_avatar_fk.sql index ee34fae0..65600cf6 100644 --- a/migrations/V143__login_avatar_fk.sql +++ b/migrations/V143__login_avatar_fk.sql @@ -4,7 +4,7 @@ -- reconciler to repair any drift. ALTER TABLE login - ADD COLUMN avatar_id mediumint(8) unsigned DEFAULT NULL, + ADD COLUMN avatar_id int(11) unsigned DEFAULT NULL, ADD CONSTRAINT fk_login_avatar FOREIGN KEY (avatar_id) REFERENCES avatars_list (id) ON DELETE SET NULL ON UPDATE CASCADE;