django-field-history icon indicating copy to clipboard operation
django-field-history copied to clipboard

Use of thread local in middleware makes tests flaky

Open twschiller opened this issue 4 years ago • 1 comments

We had an issue in https://github.com/twschiller/open-synthesis/issues/214 where we'd get a database integrity error about the FieldHistory referencing users that didn't exist

We had to add the following to our base classes to reset the request context after tests where we used the Django test client with a logged in user

 def tearDown(self) -> None:
        FieldHistoryTracker.thread.request = None

twschiller avatar May 09 '20 13:05 twschiller

Thanks @twschiller

I had this exact issue and your solution worked for me too.

For others looking here, for me that error was:

django.db.utils.IntegrityError: insert or update on table "field_history_fieldhistory" violates foreign key constraint "field_history_fieldhistory_user_id_81e3dc7d_fk_auth_user_id" DETAIL: Key (user_id)=(7) is not present in table "auth_user".

matthewslaney avatar May 31 '20 04:05 matthewslaney