From 70d71b3107249e44978605e67012e3f7d01d2812 Mon Sep 17 00:00:00 2001 From: Staci Mullins <63313398+stacimc@users.noreply.github.com> Date: Mon, 24 Jun 2024 11:01:26 -0700 Subject: [PATCH] Drop FK constraint on media_obj in MediaDecisionThrough, update backfillmoderationdecision command (#4530) * Remove foreign key constraint on media_obj from decision through table * Create MediaDecisionThrough objects using identifier rather than instance * On delete DO_NOTHING --- .../commands/backfillmoderationdecision.py | 4 +++- ...audiodecisionthrough_media_obj_and_more.py | 24 +++++++++++++++++++ api/api/models/audio.py | 3 ++- api/api/models/image.py | 3 ++- api/api/models/media.py | 3 ++- api/latest_migrations/api | 2 +- 6 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 api/api/migrations/0067_alter_audiodecisionthrough_media_obj_and_more.py diff --git a/api/api/management/commands/backfillmoderationdecision.py b/api/api/management/commands/backfillmoderationdecision.py index 2b3598a3a66..e7d704e6d0b 100644 --- a/api/api/management/commands/backfillmoderationdecision.py +++ b/api/api/management/commands/backfillmoderationdecision.py @@ -94,7 +94,9 @@ def handle(self, *args, **options): MediaReport.objects.bulk_update(reports_chunk, ["decision"]) MediaDecisionThrough.objects.bulk_create( [ - MediaDecisionThrough(media_obj=report.media_obj, decision=decision) + MediaDecisionThrough( + media_obj_id=report.media_obj_id, decision=decision + ) for report, decision in zip(reports_chunk, decisions) ] ) diff --git a/api/api/migrations/0067_alter_audiodecisionthrough_media_obj_and_more.py b/api/api/migrations/0067_alter_audiodecisionthrough_media_obj_and_more.py new file mode 100644 index 00000000000..c052f702e73 --- /dev/null +++ b/api/api/migrations/0067_alter_audiodecisionthrough_media_obj_and_more.py @@ -0,0 +1,24 @@ +# Generated by Django 4.2.11 on 2024-06-24 17:47 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0066_rename_contentprovider_to_contentsource'), + ] + + operations = [ + migrations.AlterField( + model_name='audiodecisionthrough', + name='media_obj', + field=models.ForeignKey(db_column='identifier', db_constraint=False, on_delete=django.db.models.deletion.DO_NOTHING, to='api.audio', to_field='identifier'), + ), + migrations.AlterField( + model_name='imagedecisionthrough', + name='media_obj', + field=models.ForeignKey(db_column='identifier', db_constraint=False, on_delete=django.db.models.deletion.DO_NOTHING, to='api.image', to_field='identifier'), + ), + ] diff --git a/api/api/models/audio.py b/api/api/models/audio.py index f1863ea4060..3f7a4637b08 100644 --- a/api/api/models/audio.py +++ b/api/api/models/audio.py @@ -366,8 +366,9 @@ class AudioDecisionThrough(AbstractMediaDecisionThrough): media_obj = models.ForeignKey( Audio, to_field="identifier", - on_delete=models.CASCADE, + on_delete=models.DO_NOTHING, db_column="identifier", + db_constraint=False, ) decision = models.ForeignKey(AudioDecision, on_delete=models.CASCADE) diff --git a/api/api/models/image.py b/api/api/models/image.py index 3d361d473f7..873f7ad3a69 100644 --- a/api/api/models/image.py +++ b/api/api/models/image.py @@ -169,8 +169,9 @@ class ImageDecisionThrough(AbstractMediaDecisionThrough): media_obj = models.ForeignKey( Image, to_field="identifier", - on_delete=models.CASCADE, + on_delete=models.DO_NOTHING, db_column="identifier", + db_constraint=False, ) decision = models.ForeignKey(ImageDecision, on_delete=models.CASCADE) diff --git a/api/api/models/media.py b/api/api/models/media.py index 86352967b27..2bccfea1124 100644 --- a/api/api/models/media.py +++ b/api/api/models/media.py @@ -352,8 +352,9 @@ class AbstractMediaDecisionThrough(models.Model): media_obj = models.ForeignKey( AbstractMedia, to_field="identifier", - on_delete=models.CASCADE, + on_delete=models.DO_NOTHING, db_column="identifier", + db_constraint=False, ) decision = models.ForeignKey(AbstractMediaDecision, on_delete=models.CASCADE) diff --git a/api/latest_migrations/api b/api/latest_migrations/api index 2f6c01764f5..080816ff31b 100644 --- a/api/latest_migrations/api +++ b/api/latest_migrations/api @@ -2,4 +2,4 @@ # If you have a merge conflict in this file, it means you need to run: # manage.py makemigrations --merge # in order to resolve the conflict between migrations. -0066_contentsource_delete_contentprovider +0067_alter_audiodecisionthrough_media_obj_and_more