Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The AbstractMediaDecisionThrough class and its inheriting classes shouldn't use actual foreign keys to media tables #4512

Closed
krysal opened this issue Jun 18, 2024 · 3 comments · Fixed by #4530
Assignees
Labels
💻 aspect: code Concerns the software code in the repository 🛠 goal: fix Bug fix 🟧 priority: high Stalls work on the project or its dependents 🧱 stack: api Related to the Django API

Comments

@krysal
Copy link
Member

krysal commented Jun 18, 2024

Description

When running the backfillmoderationdecision management command, it failed while bulk creating MediaDecisionThrough for images due to an image identifier not found in the image table. This is expected for the case of unindexed media (either because the medium disappeared from the source or because of a report), given that the entry is removed from the media table and the identifier moved to the api_deleted<media> table.

Error

Traceback (most recent call last):
  File "/.venv/lib/python3.11/site-packages/django/db/models/fields/related_descriptors.py", line 218, in __get__
    rel_obj = self.field.get_cached_value(instance)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.venv/lib/python3.11/site-packages/django/db/models/fields/mixins.py", line 15, in get_cached_value
    return instance._state.fields_cache[cache_name]
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: 'media_obj'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/api/manage.py", line 29, in <module>
    main()
  File "/api/manage.py", line 25, in main
    execute_from_command_line(sys.argv)
  File "/.venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/.venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/.venv/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/.venv/lib/python3.11/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/api/api/management/commands/backfillmoderationdecision.py", line 96, in handle
    [
  File "/api/api/management/commands/backfillmoderationdecision.py", line 97, in <listcomp>
    MediaDecisionThrough(media_obj=report.media_obj, decision=decision)
                                   ^^^^^^^^^^^^^^^^
  File "/.venv/lib/python3.11/site-packages/django/db/models/fields/related_descriptors.py", line 236, in __get__
    rel_obj = self.get_object(instance)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.venv/lib/python3.11/site-packages/django/db/models/fields/related_descriptors.py", line 199, in get_object
    return qs.get(self.field.get_reverse_related_filter(instance))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.venv/lib/python3.11/site-packages/django/db/models/query.py", line 637, in get
    raise self.model.DoesNotExist(
api.models.image.Image.DoesNotExist: Image matching query does not exist.
Sentry is attempting to send 2 pending events
Waiting up to 2 seconds

Expected behavior

We want to drop the foreign keys so this error does not appear and the command can finish without issues and changes.

Additional context

@krysal krysal added 🟧 priority: high Stalls work on the project or its dependents 🛠 goal: fix Bug fix 💻 aspect: code Concerns the software code in the repository 🧱 stack: api Related to the Django API labels Jun 18, 2024
@openverse-bot openverse-bot moved this to 📋 Backlog in Openverse Backlog Jun 18, 2024
@krysal krysal changed the title The AbstractMediaDecisionThrough class and its inheriting classes shouldn't use actual foreign keys The AbstractMediaDecisionThrough class and its inheriting classes shouldn't use actual foreign keys to media tables Jun 18, 2024
@stacimc
Copy link
Collaborator

stacimc commented Jun 18, 2024

I'm not sure what you mean by "we want to drop the foreign keys".

From the IPs, my understanding is that when we make a decision that deindexes a record we want to have a reference to the media_obj on all decisions and reports, even when the referenced media was deleted from its media table. We already allow this on the report model with db_constraint=False and I think we were supposed to support that on the ModerationDecision as well (although it looks like we don't 🤔).

So I think we need to:

  • In the backfill command, get the 'plain' value of the media obj id from the MediaReport, rather than trying to get the instance (maybe report.media_obj_id rather than report.media_obj?)
  • Update the MediaDecisionThrough model to have db_constraint=False <-- not sure if this requires a migration
  • When creating the MediaDecisionThrough objects, pass the 'plain' value of the identifier (since there is no instance to pass) -- something like MediaDecisionThrough(media_obj_id=report.media_obj_id ...)

Does that sound correct? Cc @sarayourfriend as well

@krysal
Copy link
Member Author

krysal commented Jun 18, 2024

@stacimc you explained it better! I was referring specifically to the part of the constraint, db_constraint=False, so your steps are correct. I didn't remember if that was possible in this case, but it looks like it is. Great.

@stacimc
Copy link
Collaborator

stacimc commented Jun 18, 2024

Perfect! Thanks for double checking me :)

@stacimc stacimc self-assigned this Jun 18, 2024
@openverse-bot openverse-bot moved this from 📋 Backlog to 📅 To Do in Openverse Backlog Jun 18, 2024
@openverse-bot openverse-bot moved this from 📅 To Do to 🏗 In Progress in Openverse Backlog Jun 20, 2024
@openverse-bot openverse-bot moved this from 🏗 In Progress to ✅ Done in Openverse Backlog Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💻 aspect: code Concerns the software code in the repository 🛠 goal: fix Bug fix 🟧 priority: high Stalls work on the project or its dependents 🧱 stack: api Related to the Django API
Projects
Archived in project
2 participants