django-easy-audit
django-easy-audit copied to clipboard
CRUDEvents added during migration, leading to IntegrityError in tests
The post_save signal handler uses the should_audit method to determine whether the model being changed should be tracked.
During migration, if model instances are created, should_audit eventually gets passed an instance of __fake__.ContentType instead of ContentType, does not recognize it as an unregistered class, and returns True.
This results in two issues:
-
Incorrect
CRUDEvententries forContentTypeobjects -
If migration happens during tests, during test tear-down the ContentType cache will be inconsistent with the actual table, leading to errors like
django.db.utils.IntegrityError: insert or update on table "easyaudit_crudevent" violates foreign key constraint "easyaudit_crudevent_content_type_id_618ed0c6_fk_django_co"
DETAIL: Key (content_type_id)=(1) is not present in table "django_content_type".
I think this is really a Django issue, but it's exposed by this library. It might be the cause of #46, too.
Would it make sense to not audit entries during migration (perhaps by checking if type(instance).__module__ == '__fake__'?)
For now, the workaround I'm using is to call ContentType.objects.clear_cache() in my test tear-down.
We haven't had an issue with the __fake__ content types, so I won't say that this isn't the issue (we just haven't seen it).
I'm prepping a PR which has a similar issue (migrations run, has an error during, and when test code runs the consequence appears): https://github.com/jheld/django-easy-audit/tree/bugfix/content-type-models-transaction