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
djvg
  • 11,722
  • 5
  • 72
  • 103
Ankit Kumar Rathod
  • 503
  • 1
  • 4
  • 7
  • 1
    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. – JwM Jan 29 '18 at 14:25
  • 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 DB – stelios Dec 11 '18 at 11:30

4 Answers4

93

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
  • 3,646
  • 26
  • 21
  • 3
    I am having this error and running `makemigrations` and `migrate` is not helping, as all migrations were already created and run. – Stuart Axon Aug 27 '20 at 15:56
  • 1
    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! – rgenito Sep 16 '20 at 06:37
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
    },
}
Griffith Rees
  • 1,285
  • 2
  • 15
  • 24
Samuel Dauzon
  • 10,744
  • 13
  • 61
  • 94
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
            },
        ),
Ihor Pomaranskyy
  • 5,437
  • 34
  • 37
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.

whatapalaver
  • 865
  • 13
  • 25