当我运行测试用例时,出现此错误: psycopg2.OperationalError: cursor "_django_curs_140351416325888_23" does
Posted
技术标签:
【中文标题】当我运行测试用例时,出现此错误: 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 【发布时间】:2018-06-19 10:32:18 【问题描述】:我正在尝试运行测试用例,但出现以下错误。
运行命令: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
【问题讨论】:
您是否使用 inspectdb 命令从预先存在的表中生成模型?我遇到了同样的问题,发现要引入预先存在的数据,我可以通过允许 django 创建我的表然后将我的数据复制到其中来解决它。 这可能是几件事,包括与当前值冲突的约束规则修改等。如果它的开发环境没有数据库中的重要数据,最简单的解决方案是重新创建数据库 【参考方案1】:就我而言,它发生在使用 PostgreSQL 的生产系统中,并且所有迁移都已完成。
将 DISABLE_SERVER_SIDE_CURSORS 设置为 True 修复我的错误:
DATABASES =
'default':
'ENGINE': 'django.db.backends.postgresql',
'USER': 'db_user',
'NAME': 'db_name',
'DISABLE_SERVER_SIDE_CURSORS': True, # <------ Only for PostgreSQL
,
【讨论】:
【参考方案2】:当我搞砸了在 Django 单元测试中使用非托管(在模型的 Meta
中使用 managed = False
)模型时,我遇到了同样的问题。
解决方案是在执行单元测试时管理模型。
为了实现这一点,我必须在迁移中应用更改,如下所示:
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
,
),
【讨论】:
【参考方案3】:遇到这个问题的时候,原来是因为我在模型中添加了字段,忘记了makemigrations
和migrate
。
在这种情况下,通常你会收到来自 Django 的警告,但由于某种原因我没有收到。
【讨论】:
我遇到了这个错误,运行makemigrations
和 migrate
没有帮助,因为所有迁移都已创建并运行。
顺便说一句,当 Seb 说“原来是因为我在模型中添加了字段而忘记进行迁移和迁移”时,这包括模型中的任何更改。说真的,django 不在这里!即使你修改了模型字段的 help_text,你也会遇到这个问题。如果 makemigrations 和 migrate 没有帮助,那么查看您的文件差异,您肯定会发现您已经以某种方式、形状或形式修改了模型,从而影响了模型的数据库字段。祝你好运!以上是关于当我运行测试用例时,出现此错误: psycopg2.OperationalError: cursor "_django_curs_140351416325888_23" does 的主要内容,如果未能解决你的问题,请参考以下文章
在套件中运行测试用例时在 XML testng 文件中出现错误
当我尝试运行测试用例时,它显示了 shallowMount 错误,如何修复 VUE.JS 中的 ShallowMount 错误?