访问方法 "gin" 的操作符类 "gin_trgm_ops" 不存在

0 投票
0 回答
32 浏览
提问于 2025-04-11 23:22

psycopg2.errors.UndefinedObject: operator class "gin_trgm_ops" does not exist for access method "gin"

大家好,当我尝试在我的项目上运行pytest时,出现了这个完整的错误信息。我的项目是用Python/Django写的,数据库是PostgreSQL,所有的东西都在Docker里。

我在Docker上用django-cookiecutter模板搭建了一个项目,所有设置都是默认的。我在一个字符串字段上添加了一个gin索引,迁移操作成功执行,pg_trgm扩展也成功创建了,但当我用pytest测试我的项目时,就出现了这个错误。

这是我的pytest.ini文件:

[pytest]
DJANGO_SETTINGS_MODULE = config.settings.test

这是我测试设置文件中的数据库配置:test.py

DATABASES['test'] = { # noqa
    'ENGINE': 'django.contrib.gis.db.backends.postgis',
    'NAME': 'test',
    'PASSWORD': 'test',
    'USER': 'test',
    'HOST': 'localhost',
    'PORT': 5454,
}

这是迁移的一部分,它创建了pg_trgm扩展并在指定字段上添加了索引。

migrations.AddIndex(
            model_name='<model_name>',
            index=django.contrib.postgres.indexes.GinIndex(fields=['field_name'], name='field_name_gin_idx', opclasses=['gin_trgm_ops']),
        ),

这是我收到的完整错误追踪信息:

self = <django.db.backends.utils.CursorWrapper object at 0xffff7244dbb0>
sql = 'CREATE INDEX "bank_name_gin_idx" ON "financing_graincreditagrobankworksheet" USING gin ("bank_name" gin_trgm_ops)', params = None
ignored_wrapper_args = (False, {'connection': <django.contrib.gis.db.backends.postgis.base.DatabaseWrapper object at 0xffff7d096b80>, 'cursor': <django.db.backends.utils.CursorWrapper object at 0xffff7244dbb0>})

    def _execute(self, sql, params, *ignored_wrapper_args):
        self.db.validate_no_broken_transaction()
        with self.db.wrap_database_errors:
            if params is None:
                # params default might be backend specific.
>               return self.cursor.execute(sql)
E               psycopg2.errors.UndefinedObject: operator class "gin_trgm_ops" does not exist for access method "gin"

/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py:82: UndefinedObject

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

request = <SubRequest '_django_setup_unittest' for <TestCaseFunction test_correct_insurance_company_name>>
django_db_blocker = <pytest_django.plugin._DatabaseBlocker object at 0xffff7fe702b0>

    @pytest.fixture(autouse=True, scope="class")
    def _django_setup_unittest(
        request,
        django_db_blocker: "_DatabaseBlocker",
    ) -> Generator[None, None, None]:
        """Setup a django unittest, internal to pytest-django."""
        if not django_settings_is_configured() or not is_django_unittest(request):
            yield
            return
    
        # Fix/patch pytest.
        # Before pytest 5.4: https://github.com/pytest-dev/pytest/issues/5991
        # After pytest 5.4: https://github.com/pytest-dev/pytest-django/issues/824
        from _pytest.unittest import TestCaseFunction
        original_runtest = TestCaseFunction.runtest
    
        def non_debugging_runtest(self) -> None:
            self._testcase(result=self)
    
        try:
            TestCaseFunction.runtest = non_debugging_runtest  # type: ignore[assignment]
    
>           request.getfixturevalue("django_db_setup")

/usr/local/lib/python3.9/site-packages/pytest_django/plugin.py:490: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/local/lib/python3.9/site-packages/pytest_django/fixtures.py:122: in django_db_setup
    db_cfg = setup_databases(
/usr/local/lib/python3.9/site-packages/django/test/utils.py:179: in setup_databases
    connection.creation.create_test_db(
/usr/local/lib/python3.9/site-packages/django/db/backends/base/creation.py:74: in create_test_db
    call_command(
/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py:181: in call_command
    return command.execute(*args, **defaults)
/usr/local/lib/python3.9/site-packages/django/core/management/base.py:398: in execute
    output = self.handle(*args, **options)
/usr/local/lib/python3.9/site-packages/django/core/management/base.py:89: in wrapped
    res = handle_func(*args, **kwargs)
/usr/local/lib/python3.9/site-packages/django/core/management/commands/migrate.py:244: in handle
    post_migrate_state = executor.migrate(
/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py:117: in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py:147: in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py:227: in apply_migration
    state = migration.apply(state, schema_editor)
/usr/local/lib/python3.9/site-packages/django/db/migrations/migration.py:126: in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
/usr/local/lib/python3.9/site-packages/django/db/migrations/operations/models.py:761: in database_forwards
    schema_editor.add_index(model, self.index)
/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/schema.py:218: in add_index
    self.execute(index.create_sql(model, self, concurrently=concurrently), params=None)
/usr/local/lib/python3.9/site-packages/django/db/backends/base/schema.py:145: in execute
    cursor.execute(sql, params)
/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py:66: in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py:75: in _execute_with_wrappers
    return executor(sql, params, many, context)
/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py:84: in _execute
    return self.cursor.execute(sql, params)
/usr/local/lib/python3.9/site-packages/django/db/utils.py:90: in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <django.db.backends.utils.CursorWrapper object at 0xffff7244dbb0>
sql = 'CREATE INDEX "bank_name_gin_idx" ON "financing_graincreditagrobankworksheet" USING gin ("bank_name" gin_trgm_ops)', params = None
ignored_wrapper_args = (False, {'connection': <django.contrib.gis.db.backends.postgis.base.DatabaseWrapper object at 0xffff7d096b80>, 'cursor': <django.db.backends.utils.CursorWrapper object at 0xffff7244dbb0>})

    def _execute(self, sql, params, *ignored_wrapper_args):
        self.db.validate_no_broken_transaction()
        with self.db.wrap_database_errors:
            if params is None:
                # params default might be backend specific.
>               return self.cursor.execute(sql)
E               django.db.utils.ProgrammingError: operator class "gin_trgm_ops" does not exist for access method "gin"

/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py:82: ProgrammingError

我想用pytest运行测试,但遇到了这个问题。实际上,这个问题在项目运行时并没有造成任何影响,gin索引是正常工作的。但是无论是运行Django的manage.py test还是pytest,只要测试运行就会出现上面的错误。如果我注释掉迁移中生成索引的部分,测试就能正常运行。

0 个回答

暂无回答

撰写回答