Use of thread local in middleware makes tests flaky
Opened this issue · 1 comments
twschiller commented
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
matthewslaney commented
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".