我正在尝试使用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/