From 7e0aad667113fb00da59ae40bb7d48f4d9d23d1a Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Tue, 20 Feb 2024 18:14:56 +0100
Subject: [PATCH] Fix unique constraint for group and site preferences to
 include instance

---
 aleksis/core/migrations/0035_preference_model_unique.py | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/aleksis/core/migrations/0035_preference_model_unique.py b/aleksis/core/migrations/0035_preference_model_unique.py
index a62950381..bf43c53b4 100644
--- a/aleksis/core/migrations/0035_preference_model_unique.py
+++ b/aleksis/core/migrations/0035_preference_model_unique.py
@@ -22,7 +22,8 @@ class Migration(migrations.Migration):
         for model in models:
             duplicates = {}
             for obj in model.objects.using(db_alias).all():
-                key = f"{obj.section}__{obj.name}"
+                instance = obj.instance.pk if hasattr(obj, "instance") else "site"
+                key = f"{instance}__{obj.section}__{obj.name}"
                 duplicates.setdefault(key, [])
                 duplicates[key].append(obj)
 
@@ -40,11 +41,11 @@ class Migration(migrations.Migration):
         migrations.RunPython(_migrate_preference_models_to_unique),
         migrations.AlterUniqueTogether(
             name='grouppreferencemodel',
-            unique_together={('section', 'name')},
+            unique_together={('instance', 'section', 'name')},
         ),
         migrations.AlterUniqueTogether(
             name='personpreferencemodel',
-            unique_together={('section', 'name')},
+            unique_together={('instance', 'section', 'name')},
         ),
         migrations.AlterUniqueTogether(
             name='sitepreferencemodel',
-- 
GitLab