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

4

40

I'm trying to run test cases, but I get below error.

Run command : python manage.py test

Type 'yes' if you would like to try deleting the test database 'test_project_management_db', or 'no' to cancel: yes
Destroying old test database for alias 'default'...

Traceback (most recent call last):
  File "manage.py", line 24, in <module>
    execute_from_command_line(sys.argv)
  File "/home/rails/Desktop/projects/envs/project_manage_env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/home/rails/Desktop/projects/envs/project_manage_env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/rails/Desktop/projects/envs/project_manage_env/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 29, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/home/rails/Desktop/projects/envs/project_manage_env/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/rails/Desktop/projects/envs/project_manage_env/local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/rails/Desktop/projects/envs/project_manage_env/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 62, in handle
    failures = test_runner.run_tests(test_labels)
  File "/home/rails/Desktop/projects/envs/project_manage_env/local/lib/python2.7/site-packages/django/test/runner.py", line 601, in run_tests
    old_config = self.setup_databases()
  File "/home/rails/Desktop/projects/envs/project_manage_env/local/lib/python2.7/site-packages/django/test/runner.py", line 546, in setup_databases
    self.parallel, **kwargs
  File "/home/rails/Desktop/projects/envs/project_manage_env/local/lib/python2.7/site-packages/django/test/utils.py", line 187, in setup_databases
    serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
  File "/home/rails/Desktop/projects/envs/project_manage_env/local/lib/python2.7/site-packages/django/db/backends/base/creation.py", line 77, in create_test_db
    self.connection._test_serialized_contents = self.serialize_db_to_string()
  File "/home/rails/Desktop/projects/envs/project_manage_env/local/lib/python2.7/site-packages/django/db/backends/base/creation.py", line 121, in serialize_db_to_string
    serializers.serialize("json", get_objects(), indent=None, stream=out)
  File "/home/rails/Desktop/projects/envs/project_manage_env/local/lib/python2.7/site-packages/django/core/serializers/__init__.py", line 129, in serialize
    s.serialize(queryset, **options)
  File "/home/rails/Desktop/projects/envs/project_manage_env/local/lib/python2.7/site-packages/django/core/serializers/base.py", line 80, in serialize
    for count, obj in enumerate(queryset, start=1):
  File "/home/rails/Desktop/projects/envs/project_manage_env/local/lib/python2.7/site-packages/django/db/backends/base/creation.py", line 117, in get_objects
    for obj in queryset.iterator():
  File "/home/rails/Desktop/projects/envs/project_manage_env/local/lib/python2.7/site-packages/django/db/models/query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
  File "/home/rails/Desktop/projects/envs/project_manage_env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 880, in execute_sql
    cursor.close()
psycopg2.OperationalError: cursor "_django_curs_140351416325888_23" does not exist
Hayman answered 10/1, 2018 at 9:37 Comment(2)
Did you generate your models from pre-existing tables using the inspectdb command? I encountered this same issue and found that to bring in pre-existing data I could get around it by allowing django to create my tables and then just copying my data into them.Paderna
It could be several things, including constraint rules modifications that conflict with the current values, etc. If its a dev envirnoment without important data in the DB, the easiest solution would be recreating the DBHass
C
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.

Carcassonne answered 19/3, 2018 at 15:18 Comment(2)
I am having this error and running makemigrations and migrate is not helping, as all migrations were already created and run.Cetinje
Btw, when Seb says that "it turned out to be because I had added fields to a model and forgotten to makemigrations and migrate", this include's ANY change in models. Seriously, django is not f'ing around here! Even if you modify the help_text of a model's field, you will get this issue. If makemigrations and migrate is not helping, then look at your file differences and you will surely see that you have modified a model in some way, shape, or form that affects the model's database fields. Good luck!Pithead
A
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
    },
}
Appeasement answered 22/12, 2020 at 22:23 Comment(0)
I
4

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
            },
        ),
Insulator answered 17/11, 2020 at 14:21 Comment(2)
but where you wrote this function called running_tests?Piscatorial
It's pretty trivial and you can easily find it (or write yourself). #4088753Insulator
A
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.

Admass answered 15/6, 2023 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.