Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model with manyToMany InvalidCursorName in admin

I have a problem with Django and Postres.

Python 3.5 Django 2.2.7 psycopg2 2.8.4 postgres 9.5.19

Models:

class Model1(models.Model):
    model1_name = models.CharField(max_length=20)


class Model2(models.Model):
    model2_name = models.CharField(max_length=20)
    model_keys = models.ManyToManyField(Model1, blank=True)

Postgress DB is empty:

my_base=> \d
No relations found.

I do migrations...

python3 manage.py makemigrations
project/migrations/0004_model1_model2.py
    - Create model Model1
    - Create model Model2

python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, engine_st, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying myproject.0001_initial... OK
  Applying myproject.0002_auto_20191129_1339... OK
  Applying myproject0003_auto_20191129_1340... OK
  Applying myproject.0004_model1_model2... OK
  Applying sessions.0001_initial... OK

... , but the admin panel has error (Page for create new object):

Exception Type:     InvalidCursorName
Exception Value:    cursor "_django_curs_140479737550592_1" does not exist

Does anyone know a solution to this problem? There is no such problem on SQLite.

Internal Server Error: /admin/engine_st/model2/5/change/ Traceback (most recent call last): File "/path/to/project/venv/lib/python3.5/site-packages/django/db/utils.py", line 96, in inner return func(*args, **kwargs) psycopg2.errors.InvalidCursorName: cursor "_django_curs_139632930674432_1" does not exist

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

Traceback (most recent call last):
  File "/path/to/project/venv/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1512, in cursor_iter
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "/path/to/project/venv/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1512, in <lambda>
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "/path/to/project/venv/lib/python3.5/site-packages/django/db/utils.py", line 96, in inner
    return func(*args, **kwargs)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/path/to/project/venv/lib/python3.5/site-packages/django/db/utils.py", line 96, in inner
    return func(*args, **kwargs)
django.db.utils.OperationalError: cursor "_django_curs_139632930674432_1" does not exist


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path/to/project/venv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 145, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 143, in _get_response
    response = response.render()
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/response.py", line 106, in render
    self.content = self.rendered_content
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/response.py", line 83, in rendered_content
    content = template.render(context, self._request)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/loader_tags.py", line 188, in render
    return template.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 173, in render
    return self._render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/defaulttags.py", line 309, in render
    return nodelist.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/defaulttags.py", line 309, in render
    return nodelist.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 993, in render
    return render_value_in_context(output, context)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/template/base.py", line 972, in render_value_in_context
    value = str(value)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/utils/html.py", line 388, in <lambda>
    klass.__str__ = lambda self: mark_safe(klass_str(self))
  File "/path/to/project/venv/lib/python3.5/site-packages/django/forms/boundfield.py", line 33, in __str__
    return self.as_widget()
  File "/path/to/project/venv/lib/python3.5/site-packages/django/forms/boundfield.py", line 93, in as_widget
    renderer=self.form.renderer,
  File "/path/to/project/venv/lib/python3.5/site-packages/django/forms/widgets.py", line 241, in render
    context = self.get_context(name, value, attrs)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/contrib/admin/widgets.py", line 288, in get_context
    'rendered_widget': self.widget.render(name, value, attrs),
  File "/path/to/project/venv/lib/python3.5/site-packages/django/forms/widgets.py", line 241, in render
    context = self.get_context(name, value, attrs)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/forms/widgets.py", line 680, in get_context
    context = super().get_context(name, value, attrs)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/forms/widgets.py", line 641, in get_context
    context['widget']['optgroups'] = self.optgroups(name, context['widget']['value'], attrs)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/forms/widgets.py", line 589, in optgroups
    for index, (option_value, option_label) in enumerate(self.choices):
  File "/path/to/project/venv/lib/python3.5/site-packages/django/forms/models.py", line 1137, in __iter__
    for obj in queryset:
  File "/path/to/project/venv/lib/python3.5/site-packages/django/db/models/query.py", line 341, in _iterator
    yield from self._iterable_class(self, chunked_fetch=use_chunked_fetch, chunk_size=chunk_size)
  File "/path/to/project/venv/lib/python3.5/site-packages/django/db/models/query.py", line 72, in __iter__
    for row in compiler.results_iter(results):
  File "/path/to/project/venv/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1515, in cursor_iter
    cursor.close()
psycopg2.errors.InvalidCursorName: cursor "_django_curs_139632930674432_1" does not exist
like image 409
Kappa Sama Avatar asked Nov 29 '19 14:11

Kappa Sama


2 Answers

python manage.py migrate --run-syncdb
like image 73
drac Avatar answered Nov 16 '22 23:11

drac


Thanks @drac for your answer.

In my case it happened because the column was explicitly deleted by another user but at the same time I also removed from model and created migrations. So applying it with migrate was throwing above error as it was not finding that specific removed column in Database.

So in this case, we just have have to create tables without migrations.

You can use --run-syncdb switch which Allows creating tables for apps without migrations. While this isn’t recommended, the migrations framework is sometimes too slow on large projects with hundreds of models.

Check https://docs.djangoproject.com/en/3.1/ref/django-admin/ for more details.

Just run ran

python manage.py migrate --run-syncdb

And finally my issue got resolved.

like image 21
hygull Avatar answered Nov 17 '22 00:11

hygull