|
12 | 12 | from django.db.models.expressions import RawSQL, Value |
13 | 13 | from django.utils.encoding import force_str |
14 | 14 |
|
15 | | -from watson.models import SearchEntry, has_int_pk |
| 15 | +from watson.models import SearchEntry, has_int_pk, has_uuid_pk |
16 | 16 |
|
17 | 17 |
|
18 | 18 | def regex_from_word(word): |
@@ -282,22 +282,33 @@ def do_filter(self, engine_slug, queryset, search_text): |
282 | 282 | if has_int_pk(model): |
283 | 283 | ref_name = "object_id_int" |
284 | 284 | ref_name_typecast = "" |
| 285 | + watson_id_typecast = "" |
| 286 | + elif has_uuid_pk(model): |
| 287 | + ref_name = "object_id" |
| 288 | + # Moving the type cast happens on the watson_searchentry table. |
| 289 | + # This ensures the primary key will be properly used. |
| 290 | + ref_name_typecast = "" |
| 291 | + watson_id_typecast = "::uuid" |
285 | 292 | else: |
286 | 293 | ref_name = "object_id" |
287 | | - # Cast to text to make join work with uuid columns |
| 294 | + # Cast to text to make join work with other column types |
288 | 295 | ref_name_typecast = "::text" |
| 296 | + watson_id_typecast = "" |
| 297 | + |
289 | 298 | return queryset.extra( |
290 | 299 | tables=("watson_searchentry",), |
291 | 300 | where=( |
292 | 301 | "watson_searchentry.engine_slug = %s", |
293 | 302 | "watson_searchentry.search_tsv @@ to_tsquery('{search_config}', %s)".format( |
294 | 303 | search_config=self.search_config |
295 | 304 | ), |
296 | | - "watson_searchentry.{ref_name} = {table_name}.{pk_name}{ref_name_typecast}".format( |
| 305 | + "watson_searchentry.{ref_name}{watson_id_typecast} = {table_name}.{pk_name}{ref_name_typecast}".format( |
| 306 | + |
297 | 307 | ref_name=ref_name, |
298 | 308 | table_name=connection.ops.quote_name(model._meta.db_table), |
299 | 309 | pk_name=connection.ops.quote_name(pk.db_column or pk.attname), |
300 | | - ref_name_typecast=ref_name_typecast |
| 310 | + ref_name_typecast=ref_name_typecast, |
| 311 | + watson_id_typecast=watson_id_typecast |
301 | 312 | ), |
302 | 313 | "watson_searchentry.content_type_id = %s" |
303 | 314 | ), |
|
0 commit comments