Test result isn't stable - Key (user_id) is not present in table "auth_user". #2708

Closed
opened 2025-12-29 18:21:15 +01:00 by adam · 5 comments
Owner

Originally created by @haminhcong on GitHub (Jun 29, 2019).

Originally assigned to: @jeremystretch on GitHub.

Hi everyone. I'm learning developing netbox and I had a problem when run netbox test. Can you help me resolve this problem ? Thank youi.

Environment

  • Python version: 3.6.5
  • NetBox version: 2.6

Steps to Reproduce

  1. Clone netbox from repo git@github.com:digitalocean/netbox.git and checkout tag v2.6.0
  2. Create virtualenv, install required packages and use this virtualenv
  3. Create Postgres DB and Redis for development environment
  4. Run test by command python manage.py test

Expected Behavior

All tests passed every time I run netbox tests by command python manage.py test

Observed Behavior

Sometimes, some tests failed without have extract reason:

ERROR: test_delete_tenantgroup (tenancy.tests.test_api.TenantGroupTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
psycopg2.errors.ForeignKeyViolation: insert or update on table "extras_objectchange" violates foreign key constraint "extras_objectchange_user_id_7fdf8186_fk_auth_user_id"
DETAIL:  Key (user_id)=(739) is not present in table "auth_user".


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 274, in __call__
    self._post_teardown()
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 1009, in _post_teardown
    self._fixture_teardown()
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 1177, in _fixture_teardown
    connections[db_name].check_constraints()
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 246, in check_constraints
    self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE')
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/cacheops/transaction.py", line 99, in execute
    result = self._no_monkey.execute(self, sql, params)
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
django.db.utils.IntegrityError: insert or update on table "extras_objectchange" violates foreign key constraint "extras_objectchange_user_id_7fdf8186_fk_auth_user_id"
DETAIL:  Key (user_id)=(739) is not present in table "auth_user".


ERROR: test_delete_clustertype (virtualization.tests.test_api.ClusterTypeTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
psycopg2.errors.ForeignKeyViolation: insert or update on table "extras_objectchange" violates foreign key constraint "extras_objectchange_user_id_7fdf8186_fk_auth_user_id"
DETAIL:  Key (user_id)=(739) is not present in table "auth_user".


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 274, in __call__
    self._post_teardown()
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 1009, in _post_teardown
    self._fixture_teardown()
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 1177, in _fixture_teardown
    connections[db_name].check_constraints()
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 246, in check_constraints
    self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE')
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/cacheops/transaction.py", line 99, in execute
    result = self._no_monkey.execute(self, sql, params)
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
django.db.utils.IntegrityError: insert or update on table "extras_objectchange" violates foreign key constraint "extras_objectchange_user_id_7fdf8186_fk_auth_user_id"
DETAIL:  Key (user_id)=(739) is not present in table "auth_user".


======================================================================
ERROR: test_delete_interface (virtualization.tests.test_api.InterfaceTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
psycopg2.errors.ForeignKeyViolation: insert or update on table "extras_objectchange" violates foreign key constraint "extras_objectchange_user_id_7fdf8186_fk_auth_user_id"
DETAIL:  Key (user_id)=(739) is not present in table "auth_user".


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 274, in __call__
    self._post_teardown()
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 1009, in _post_teardown
    self._fixture_teardown()
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 1177, in _fixture_teardown
    connections[db_name].check_constraints()
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 246, in check_constraints
    self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE')
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/cacheops/transaction.py", line 99, in execute
    result = self._no_monkey.execute(self, sql, params)
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
django.db.utils.IntegrityError: insert or update on table "extras_objectchange" violates foreign key constraint "extras_objectchange_user_id_7fdf8186_fk_auth_user_id"
DETAIL:  Key (user_id)=(739) is not present in table "auth_user".


======================================================================
ERROR: test_delete_virtualmachine (virtualization.tests.test_api.VirtualMachineTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
psycopg2.errors.ForeignKeyViolation: insert or update on table "extras_objectchange" violates foreign key constraint "extras_objectchange_user_id_7fdf8186_fk_auth_user_id"
DETAIL:  Key (user_id)=(739) is not present in table "auth_user".


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 274, in __call__
    self._post_teardown()
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 1009, in _post_teardown
    self._fixture_teardown()
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 1177, in _fixture_teardown
    connections[db_name].check_constraints()
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 246, in check_constraints
    self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE')
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/cacheops/transaction.py", line 99, in execute
    result = self._no_monkey.execute(self, sql, params)
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
django.db.utils.IntegrityError: insert or update on table "extras_objectchange" violates foreign key constraint "extras_objectchange_user_id_7fdf8186_fk_auth_user_id"
DETAIL:  Key (user_id)=(739) is not present in table "auth_user".
----------------------------------------------------------------------

I tried to analyze this problem, and i saw that this problem come from object_change middleware process:

# extras/middleware.py
        # Create records for any cached objects that were created/updated.
        for obj, action in _thread_locals.changed_objects:

            # Record the change
            if hasattr(obj, 'log_change'):
                obj.log_change(request.user, request.id, action)

def cache_changed_object(instance, **kwargs):

    action = OBJECTCHANGE_ACTION_CREATE if kwargs['created'] else OBJECTCHANGE_ACTION_UPDATE

    # Cache the object for further processing was the response has completed.
    _thread_locals.changed_objects.append(
        (instance, action)
    )


def _record_object_deleted(request, instance, **kwargs):
    # Record that the object was deleted
    if hasattr(instance, 'log_change'):
        instance.log_change(request.user, request.id, OBJECTCHANGE_ACTION_DELETE)

    # Enqueue webhooks
    enqueue_webhooks(instance, request.user, request.id, OBJECTCHANGE_ACTION_DELETE)

and discovered that, sometime request context in test is not cleared after each test => request.user.id is not reset after each test, but this user_id is not exist in failed tests, because it is cleared with post_teardown in previous test.

Anh when i put some debug in code

    def test_delete_tenantgroup(self):
        print('Test delete tenant group stats: \n')
        print('Current Test User ID {}: \n'.format(self.user.pk))
        url = reverse('tenancy-api:tenantgroup-detail', kwargs={'pk': self.tenantgroup1.pk})
        response = self.client.delete(url, **self.header)
        self.assertHttpStatus(response, status.HTTP_204_NO_CONTENT)
        print('Tenant Group Deleted')
        self.assertEqual(TenantGroup.objects.count(), 2)

def _record_object_deleted(request, instance, **kwargs):

    print("Record Object Deleted - Current Request URL: {} - Request Action: {} - Current Request User: {}".format(
        request.path, request.method, request.user.pk))
    # Record that the object was deleted
    if hasattr(instance, 'log_change'):
        instance.log_change(request.user, request.id, OBJECTCHANGE_ACTION_DELETE)

    # Enqueue webhooks
    enqueue_webhooks(instance, request.user, request.id, OBJECTCHANGE_ACTION_DELETE)

    # Increment metric counters
    model_deletes.labels(instance._meta.model_name).inc()

it show this result:

...Test delete tenant group stats: 

Current Test User ID 755: 

Record Object Deleted - Current Request URL: /service/services/306/edit/ - Request Action: POST - Current Request User: 739
Tenant Group Deleted
.E......

=> request.user and request.path is not reset after test in this test run.

And an my notice is that this problem is not happend in everytime i ran test command. In 100 times i ran python manage.py test command, this problem is only occur in 4-5 times
Thanks for your reading

Originally created by @haminhcong on GitHub (Jun 29, 2019). Originally assigned to: @jeremystretch on GitHub. Hi everyone. I'm learning developing netbox and I had a problem when run netbox test. Can you help me resolve this problem ? Thank youi. ### Environment * Python version: 3.6.5 * NetBox version: 2.6 ### Steps to Reproduce 1. Clone netbox from repo git@github.com:digitalocean/netbox.git and checkout tag v2.6.0 2. Create virtualenv, install required packages and use this virtualenv 3. Create Postgres DB and Redis for development environment 4. Run test by command `python manage.py test` <!-- What did you expect to happen? --> ### Expected Behavior All tests passed every time I run netbox tests by command `python manage.py test` <!-- What happened instead? --> ### Observed Behavior Sometimes, some tests failed without have extract reason: ```log ERROR: test_delete_tenantgroup (tenancy.tests.test_api.TenantGroupTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute return self.cursor.execute(sql) psycopg2.errors.ForeignKeyViolation: insert or update on table "extras_objectchange" violates foreign key constraint "extras_objectchange_user_id_7fdf8186_fk_auth_user_id" DETAIL: Key (user_id)=(739) is not present in table "auth_user". The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 274, in __call__ self._post_teardown() File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 1009, in _post_teardown self._fixture_teardown() File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 1177, in _fixture_teardown connections[db_name].check_constraints() File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 246, in check_constraints self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE') File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/cacheops/transaction.py", line 99, in execute result = self._no_monkey.execute(self, sql, params) File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers return executor(sql, params, many, context) File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute return self.cursor.execute(sql) django.db.utils.IntegrityError: insert or update on table "extras_objectchange" violates foreign key constraint "extras_objectchange_user_id_7fdf8186_fk_auth_user_id" DETAIL: Key (user_id)=(739) is not present in table "auth_user". ERROR: test_delete_clustertype (virtualization.tests.test_api.ClusterTypeTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute return self.cursor.execute(sql) psycopg2.errors.ForeignKeyViolation: insert or update on table "extras_objectchange" violates foreign key constraint "extras_objectchange_user_id_7fdf8186_fk_auth_user_id" DETAIL: Key (user_id)=(739) is not present in table "auth_user". The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 274, in __call__ self._post_teardown() File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 1009, in _post_teardown self._fixture_teardown() File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 1177, in _fixture_teardown connections[db_name].check_constraints() File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 246, in check_constraints self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE') File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/cacheops/transaction.py", line 99, in execute result = self._no_monkey.execute(self, sql, params) File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers return executor(sql, params, many, context) File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute return self.cursor.execute(sql) django.db.utils.IntegrityError: insert or update on table "extras_objectchange" violates foreign key constraint "extras_objectchange_user_id_7fdf8186_fk_auth_user_id" DETAIL: Key (user_id)=(739) is not present in table "auth_user". ====================================================================== ERROR: test_delete_interface (virtualization.tests.test_api.InterfaceTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute return self.cursor.execute(sql) psycopg2.errors.ForeignKeyViolation: insert or update on table "extras_objectchange" violates foreign key constraint "extras_objectchange_user_id_7fdf8186_fk_auth_user_id" DETAIL: Key (user_id)=(739) is not present in table "auth_user". The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 274, in __call__ self._post_teardown() File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 1009, in _post_teardown self._fixture_teardown() File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 1177, in _fixture_teardown connections[db_name].check_constraints() File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 246, in check_constraints self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE') File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/cacheops/transaction.py", line 99, in execute result = self._no_monkey.execute(self, sql, params) File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers return executor(sql, params, many, context) File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute return self.cursor.execute(sql) django.db.utils.IntegrityError: insert or update on table "extras_objectchange" violates foreign key constraint "extras_objectchange_user_id_7fdf8186_fk_auth_user_id" DETAIL: Key (user_id)=(739) is not present in table "auth_user". ====================================================================== ERROR: test_delete_virtualmachine (virtualization.tests.test_api.VirtualMachineTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute return self.cursor.execute(sql) psycopg2.errors.ForeignKeyViolation: insert or update on table "extras_objectchange" violates foreign key constraint "extras_objectchange_user_id_7fdf8186_fk_auth_user_id" DETAIL: Key (user_id)=(739) is not present in table "auth_user". The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 274, in __call__ self._post_teardown() File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 1009, in _post_teardown self._fixture_teardown() File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/test/testcases.py", line 1177, in _fixture_teardown connections[db_name].check_constraints() File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 246, in check_constraints self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE') File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/cacheops/transaction.py", line 99, in execute result = self._no_monkey.execute(self, sql, params) File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers return executor(sql, params, many, context) File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/test_env/git-repo/netbox/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute return self.cursor.execute(sql) django.db.utils.IntegrityError: insert or update on table "extras_objectchange" violates foreign key constraint "extras_objectchange_user_id_7fdf8186_fk_auth_user_id" DETAIL: Key (user_id)=(739) is not present in table "auth_user". ---------------------------------------------------------------------- ``` I tried to analyze this problem, and i saw that this problem come from `object_change` middleware process: ```python # extras/middleware.py # Create records for any cached objects that were created/updated. for obj, action in _thread_locals.changed_objects: # Record the change if hasattr(obj, 'log_change'): obj.log_change(request.user, request.id, action) def cache_changed_object(instance, **kwargs): action = OBJECTCHANGE_ACTION_CREATE if kwargs['created'] else OBJECTCHANGE_ACTION_UPDATE # Cache the object for further processing was the response has completed. _thread_locals.changed_objects.append( (instance, action) ) def _record_object_deleted(request, instance, **kwargs): # Record that the object was deleted if hasattr(instance, 'log_change'): instance.log_change(request.user, request.id, OBJECTCHANGE_ACTION_DELETE) # Enqueue webhooks enqueue_webhooks(instance, request.user, request.id, OBJECTCHANGE_ACTION_DELETE) ``` and discovered that, sometime `request` context in test is not cleared after each test => `request.user.id` is not reset after each test, but this `user_id` is not exist in failed tests, because it is cleared with `post_teardown` in previous test. Anh when i put some debug in code ```python def test_delete_tenantgroup(self): print('Test delete tenant group stats: \n') print('Current Test User ID {}: \n'.format(self.user.pk)) url = reverse('tenancy-api:tenantgroup-detail', kwargs={'pk': self.tenantgroup1.pk}) response = self.client.delete(url, **self.header) self.assertHttpStatus(response, status.HTTP_204_NO_CONTENT) print('Tenant Group Deleted') self.assertEqual(TenantGroup.objects.count(), 2) def _record_object_deleted(request, instance, **kwargs): print("Record Object Deleted - Current Request URL: {} - Request Action: {} - Current Request User: {}".format( request.path, request.method, request.user.pk)) # Record that the object was deleted if hasattr(instance, 'log_change'): instance.log_change(request.user, request.id, OBJECTCHANGE_ACTION_DELETE) # Enqueue webhooks enqueue_webhooks(instance, request.user, request.id, OBJECTCHANGE_ACTION_DELETE) # Increment metric counters model_deletes.labels(instance._meta.model_name).inc() ``` it show this result: ```log ...Test delete tenant group stats: Current Test User ID 755: Record Object Deleted - Current Request URL: /service/services/306/edit/ - Request Action: POST - Current Request User: 739 Tenant Group Deleted .E...... ``` => request.user and request.path is not reset after test in this test run. And an my notice is that this problem is not happend in everytime i ran test command. In 100 times i ran `python manage.py test` command, this problem is only occur in 4-5 times Thanks for your reading
adam added the type: bugstatus: accepted labels 2025-12-29 18:21:15 +01:00
adam closed this issue 2025-12-29 18:21:15 +01:00
Author
Owner

@tovin07 commented on GitHub (Jun 29, 2019):

Same here, randomly happen.

I saw the same test result here: https://api.travis-ci.org/v3/job/510193607/log.txt

@tovin07 commented on GitHub (Jun 29, 2019): Same here, randomly happen. I saw the same test result here: https://api.travis-ci.org/v3/job/510193607/log.txt
Author
Owner

@toniengelhardt commented on GitHub (Sep 26, 2019):

I have the same problem with pytest, but it occurs everytime and only when I run more than one test function in a test class in one go.

@toniengelhardt commented on GitHub (Sep 26, 2019): I have the same problem with pytest, but it occurs everytime and only when I run more than one test function in a test class in one go.
Author
Owner

@jeremystretch commented on GitHub (Oct 22, 2019):

I think the root issue here is that the Django user that gets created when the test is run is being deleted before the ObjectChange gets written to the database. We might be able to fix this by switching from APITestCase to APITransactionTestCase. I'm going to push a commit for this, but given the squirrely nature of this bug it make take some time to verify its effect.

@jeremystretch commented on GitHub (Oct 22, 2019): I think the root issue here is that the Django user that gets created when the test is run is being deleted _before_ the ObjectChange gets written to the database. We _might_ be able to fix this by switching from `APITestCase` to `APITransactionTestCase`. I'm going to push a commit for this, but given the squirrely nature of this bug it make take some time to verify its effect.
Author
Owner

@jeremystretch commented on GitHub (Oct 22, 2019):

Turns out that APITransactionTestCase takes much longer to run, so it's best avoided if we can. As an alternate approach, I've introduced a tearDown() method on our custom APITestCase that explicitly deletes the test user. Hopefully that will negate the race condition.

@jeremystretch commented on GitHub (Oct 22, 2019): Turns out that `APITransactionTestCase` takes _much_ longer to run, so it's best avoided if we can. As an alternate approach, I've introduced a `tearDown()` method on our custom `APITestCase` that explicitly deletes the test user. Hopefully that will negate the race condition.
Author
Owner

@jeremystretch commented on GitHub (Oct 22, 2019):

As an alternate approach, I've introduced a tearDown() method on our custom APITestCase that explicitly deletes the test user. Hopefully that will negate the race condition.

That didn't work. Will have to keep digging...

@jeremystretch commented on GitHub (Oct 22, 2019): > As an alternate approach, I've introduced a `tearDown()` method on our custom `APITestCase` that explicitly deletes the test user. Hopefully that will negate the race condition. That didn't work. Will have to keep digging...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#2708