fix(importer): avoid guardian lru_cache poisoning; include M2M through tables in check_constraints

clear_cache() inside the import transaction emptied Django's ContentType
manager cache while fixture PKs were live, causing downstream ContentType
lookups to repopulate guardian's separate @lru_cache(None) with
fixture-PK objects. After the TestCase transaction rolled back to
original PKs, guardian's lru_cache held stale fixture ContentType
objects, causing MixedContentTypeError in unrelated subsequent tests.

Remove clear_cache() since it was defending against a theoretical
stale-cache scenario that doesn't occur in a proper same-install restore.

Fix check_constraints() to explicitly include auto-created M2M through
tables (populated by .set() after bulk_create) alongside the model tables,
addressing the gap where join-table FK violations would have gone
undetected.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Trenton Holmes
2026-05-04 14:12:48 -07:00
committed by stumpylog
co-authored by Claude Sonnet 4.6
parent 1b042d5513
commit 9bdf27bc90
@@ -383,13 +383,19 @@ class Command(CryptMixin, PaperlessCommand):
flush_all()
# Stale ContentType objects cached in Python would cause lookups
# against the freshly re-imported rows to return wrong PKs.
ContentType.objects.clear_cache()
# Verify referential integrity now that all rows are inserted,
# including M2M through tables written by .set() above.
connection.check_constraints()
through_tables = {
field.remote_field.through._meta.db_table
for model in loaded_models
for field in model._meta.many_to_many
if field.remote_field.through._meta.auto_created
}
table_names = [m._meta.db_table for m in loaded_models] + list(
through_tables,
)
if table_names:
connection.check_constraints(table_names=table_names)
# Sequences must be reset after inserting rows with explicit PKs
# or the next auto-increment insert will collide with an existing PK.