From 60d6d758fdadd7dea1644e36b9b0911b35e56271 Mon Sep 17 00:00:00 2001 From: Samuel Cattini-Schultz Date: Sat, 1 Jun 2024 00:32:22 +1000 Subject: [PATCH] Add unique constraint for memberships --- .../0021_membership_unique_memberships.py | 19 +++++++++++++++++++ leaderboards/models.py | 6 ++++++ 2 files changed, 25 insertions(+) create mode 100644 leaderboards/migrations/0021_membership_unique_memberships.py diff --git a/leaderboards/migrations/0021_membership_unique_memberships.py b/leaderboards/migrations/0021_membership_unique_memberships.py new file mode 100644 index 0000000..dff46aa --- /dev/null +++ b/leaderboards/migrations/0021_membership_unique_memberships.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.11 on 2024-05-31 14:28 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("leaderboards", "0020_membershipscore_performance_total_and_more"), + ] + + operations = [ + migrations.AddConstraint( + model_name="membership", + constraint=models.UniqueConstraint( + fields=("leaderboard_id", "user_id"), name="unique_memberships" + ), + ), + ] diff --git a/leaderboards/models.py b/leaderboards/models.py index 553ce73..17b4cf2 100644 --- a/leaderboards/models.py +++ b/leaderboards/models.py @@ -200,6 +200,12 @@ def __str__(self): return f"{self.leaderboard}: {self.user.username}" class Meta: + constraints = [ + models.UniqueConstraint( + fields=["leaderboard_id", "user_id"], name="unique_memberships" + ) + ] + indexes = [models.Index(fields=["leaderboard"]), models.Index(fields=["user"])]