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

95👍

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.

👤seb

10👍

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
    },
}

3👍

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
            },
        ),

0👍

I’ve just run into this problem with Django 2.2.28 and it turns out I had an incorrectly named my migration file with a .py.py extension. Correcting that enabled the tests to run through smoothly.

Leave a comment