Skip to content

clear ContentType cache in import database#246

Merged
paigewilliams merged 2 commits intodevelopfrom
debug-interruptable-upload-lookup
Mar 18, 2026
Merged

clear ContentType cache in import database#246
paigewilliams merged 2 commits intodevelopfrom
debug-interruptable-upload-lookup

Conversation

@paigewilliams
Copy link
Collaborator

@paigewilliams paigewilliams commented Mar 17, 2026

I ran into a bug with interruptible uploads after doing an import of a database. The root cause of the problem was that the pk's for the Media type were being cached, so that after doing an import of a database, the pks now change, and the cache is not cleared and thus referencing the pre-import pk.

Here is an example of the steps I saw for the problem:

  1. Load up the app with a fresh database
  2. The content type id for the Media model is 20.
  3. Create a media record, upload a media file -> success.
  4. Import a database. Media pk from import is now 11. The technique lookup pk is now 20
  5. Create a media record, looking for pk 20, causes error that technique does not have a mediafile field -> failure

Because the import database functionality is TEK DB specific, I thought that clearing the cache was a preferred approach, rather than making the change in django-resumable-async-upload, but I am open to other opinions.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a post-database-import bug where Django’s in-process ContentType cache can retain stale content type PKs, causing resumable upload code paths to reference the wrong content_type_id after an import.

Changes:

  • Clear Django’s ContentType in-memory cache immediately after loaddata completes in the database import flow.
  • Add explanatory inline comments documenting the failure mode and why the cache clear is required.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +226 to +232
# Clear Django's in-memory ContentType cache so subsequent
# requests use the PKs from the newly imported data, not
# the stale pre-import values. Without this, the
# ResumableAdminWidget renders the wrong content_type_id
# (e.g. the old Media PK now maps to LookupTechniques),
# causing FieldDoesNotExist on upload.
ContentType.objects.clear_cache()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rhodges this copilot comment is kinda relevant. It's likely spot on with the "force an app reload/restart after successful import" comment.

Comment on lines 225 to +232
management.call_command("loaddata", fixture_file_path)
# Clear Django's in-memory ContentType cache so subsequent
# requests use the PKs from the newly imported data, not
# the stale pre-import values. Without this, the
# ResumableAdminWidget renders the wrong content_type_id
# (e.g. the old Media PK now maps to LookupTechniques),
# causing FieldDoesNotExist on upload.
ContentType.objects.clear_cache()
@paigewilliams paigewilliams merged commit 7cc073a into develop Mar 18, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants