From 61c583e4974d589cec06f0d42437a46203981cc2 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Sun, 22 Mar 2026 13:03:13 -0400 Subject: [PATCH] [videos] Make imdb_id and youtube_id unique together --- .../0026_unique_imdb_youtube_together.py | 30 +++++++++++++++++++ vrobbler/apps/videos/models.py | 12 +++++++- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 vrobbler/apps/videos/migrations/0026_unique_imdb_youtube_together.py diff --git a/vrobbler/apps/videos/migrations/0026_unique_imdb_youtube_together.py b/vrobbler/apps/videos/migrations/0026_unique_imdb_youtube_together.py new file mode 100644 index 0000000..7842e83 --- /dev/null +++ b/vrobbler/apps/videos/migrations/0026_unique_imdb_youtube_together.py @@ -0,0 +1,30 @@ +# Generated by Django 4.2.29 on 2026-03-22 17:02 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("videos", "0025_add_imdb_id_unique"), + ] + + operations = [ + migrations.AlterField( + model_name="video", + name="imdb_id", + field=models.CharField(blank=True, max_length=20, null=True), + ), + migrations.AddConstraint( + model_name="video", + constraint=models.UniqueConstraint( + condition=models.Q( + models.Q(("imdb_id__isnull", True), _negated=True), + models.Q(("youtube_id__isnull", True), _negated=True), + _connector="OR", + ), + fields=("imdb_id", "youtube_id"), + name="unique_imdb_or_youtube_id", + ), + ), + ] diff --git a/vrobbler/apps/videos/models.py b/vrobbler/apps/videos/models.py index d649334..aadcdfc 100644 --- a/vrobbler/apps/videos/models.py +++ b/vrobbler/apps/videos/models.py @@ -261,7 +261,7 @@ class Video(ScrobblableMixin): season_number = models.IntegerField(**BNULL) episode_number = models.IntegerField(**BNULL) next_imdb_id = models.CharField(max_length=20, **BNULL) - imdb_id = models.CharField(max_length=20, **BNULL, unique=True) + imdb_id = models.CharField(max_length=20, **BNULL) imdb_rating = models.FloatField(**BNULL) tmdb_rating = models.FloatField(**BNULL) cover_image = models.ImageField(upload_to="videos/video/", **BNULL) @@ -293,6 +293,16 @@ class Video(ScrobblableMixin): return f"{self.title} / {self.channel}" return self.title + class Meta: + constraints = [ + models.UniqueConstraint( + fields=["imdb_id", "youtube_id"], + name="unique_imdb_or_youtube_id", + condition=~models.Q(imdb_id__isnull=True) + | ~models.Q(youtube_id__isnull=True), + ), + ] + def get_absolute_url(self): return reverse("videos:video_detail", kwargs={"slug": self.uuid})