summaryrefslogtreecommitdiff
path: root/django/contrib/postgres/search.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2018-07-20 21:23:05 -0400
committerSimon Charette <charette.s@gmail.com>2018-07-21 21:32:21 -0400
commit1a28dc3887e8d66d5e3ff08cf7fb0a6212b873e5 (patch)
tree24f4707d1237abd34bd4eeee2005868c70f7134c /django/contrib/postgres/search.py
parent71a739f3d752015504d455bf6f01e63a04d1a383 (diff)
downloaddjango-1a28dc3887e8d66d5e3ff08cf7fb0a6212b873e5.tar.gz
Fixed #29582 -- Fixed a crash when using SearchVector with non text-fields.
The PostgreSQL concat() function handles nulls and non-text values better than the || operator.
Diffstat (limited to 'django/contrib/postgres/search.py')
-rw-r--r--django/contrib/postgres/search.py9
1 files changed, 3 insertions, 6 deletions
diff --git a/django/contrib/postgres/search.py b/django/contrib/postgres/search.py
index a14d510208..7bda2291a0 100644
--- a/django/contrib/postgres/search.py
+++ b/django/contrib/postgres/search.py
@@ -1,6 +1,5 @@
from django.db.models import Field, FloatField
from django.db.models.expressions import CombinedExpression, Func, Value
-from django.db.models.functions import Coalesce
from django.db.models.lookups import Lookup
@@ -46,15 +45,13 @@ class SearchVectorCombinable:
class SearchVector(SearchVectorCombinable, Func):
function = 'to_tsvector'
- arg_joiner = " || ' ' || "
+ arg_joiner = ", ' ',"
+ template = '%(function)s(concat(%(expressions)s))'
output_field = SearchVectorField()
config = None
def __init__(self, *expressions, **extra):
super().__init__(*expressions, **extra)
- self.source_expressions = [
- Coalesce(expression, Value('')) for expression in self.source_expressions
- ]
self.config = self.extra.get('config', self.config)
weight = self.extra.get('weight')
if weight is not None and not hasattr(weight, 'resolve_expression'):
@@ -75,7 +72,7 @@ class SearchVector(SearchVectorCombinable, Func):
if template is None:
if self.config:
config_sql, config_params = compiler.compile(self.config)
- template = "%(function)s({}::regconfig, %(expressions)s)".format(config_sql.replace('%', '%%'))
+ template = "%(function)s({}::regconfig, concat(%(expressions)s))".format(config_sql.replace('%', '%%'))
else:
template = self.template
sql, params = super().as_sql(compiler, connection, function=function, template=template)