python - 提示 : No function matches the given name and argument types. 您可能需要添加显式类型转换。 TrigramSimilarity Django

标签 python django python-3.x postgresql

我正在尝试使用TrigramSimilarity进行全文搜索。我已在 PostgreSQL 中激活 pg_trgm 扩展。并在我安装的应用程序中添加了 django.contrib.postgres 。但是当我尝试查询时

blog.objects.annotate(similarity=TrigramSimilarity('name', 'abc'),).filter(similarity__gt=0.3).order_by('-similarity')

但是它给了我一个错误

psycopg2.ProgrammingError: function similarity(character varying, unknown) does not exist LINE 1: ...others_2", "blog_mod", SIMILARITY... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.

为什么会发生这个错误。请帮助我。

最佳答案

该错误表明扩展尚未安装。我不知道为什么会这样。可能您没有在用于 django 的数据库中安装扩展?

可以从 django 代码执行原始 SQL 查询。这将确保您定位正确的数据库/架构。

from django.db import connection
with connection.cursor() as cursor:
    cursor.execute('CREATE EXTENSION IF NOT EXISTS pg_trgm')

您还可以使用 Django 的自定义 TrigramExtension 迁移。创建扩展查询是相同的。但是,如果您在还需要启用 pg_trgm 的测试数据库上运行集成测试,那么迁移是一个好主意。

https://docs.djangoproject.com/en/2.1/ref/contrib/postgres/operations/#database-migration-operations

关于python - 提示 : No function matches the given name and argument types. 您可能需要添加显式类型转换。 TrigramSimilarity Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52883735/

相关文章:

python - Django CORS X-FirePHP 版本

django - 用于解析 django 异常的 Logstash conf 文件

python - 如何将函数导入到 Python 包中而不同时导入包含该函数的文件?

python - 国际化django模板中的多个复数变量

python - 只是用 lambda 尝试/排除 - Python?

python - 根据过去 7 天在 django 中创建权重逻辑

python-3.x - Microsoft Speech to Text Python SDK SPXERR_INVALID_HEADER 问题

python - 是否可以对分块数据使用 DictVectorizer?

python - 从质数计算非质因数

python - 安装 python-docx 时出错