When I run test cases I get this error: psycopg2.OperationalError: cursor "_django_curs_140351416325888_23" does not exist When I run test cases I get this error: psycopg2.OperationalError: cursor "_django_curs_140351416325888_23" does not exist django django

When I run test cases I get this error: psycopg2.OperationalError: cursor "_django_curs_140351416325888_23" does not exist


When I encountered this problem, it turned out to be because I had added fields to a model, and forgotten to makemigrations and migrate.

Normally you get a warning from Django when this is the case, but for some reason I wasn't getting one.


In my case, it's happen in a production system with PostgreSQL and all migrations done.

Set DISABLE_SERVER_SIDE_CURSORS at True fix my errors :

DATABASES = {    'default': {        'ENGINE': 'django.db.backends.postgresql',        'USER': 'db_user',        'NAME': 'db_name',        'DISABLE_SERVER_SIDE_CURSORS': True,   # <------ Only for PostgreSQL    },}


I faced the same issue when I messed with getting it possible to use unmanaged (with managed = False in model's Meta) models in Django unittests.

The solution was to make the models managed when unittests are executed.

To achieve this, I had to apply changes in migration, like this:

        migrations.CreateModel(            name='UmnamagedModelName',            fields=[                ('uuid', models.TextField(primary_key=True, serialize=False)),                # ...                ('csms_updated', models.NullBooleanField()),            ],            options={                'db_table': 'umnamage_table_name',                'managed': running_tests(),  # <= this function returns True when running tests            },        ),