etianen / django-watson

Full-text multi-table search application for Django. Easy to install and use, with good performance.
BSD 3-Clause "New" or "Revised" License
1.21k stars 129 forks source link

Use of unqualified "id" column in _create_model_filter #308

Open ercpe opened 3 months ago

ercpe commented 3 months ago

https://github.com/etianen/django-watson/blob/master/watson/search.py#L568 creates a unqualified Cast for the primary key column, which fails if the QuerySet has JOINs (e.g. because it's already filtered):

SELECT ("id"::text) AS "watson_pk_str"
FROM "model_a_table" U0 
INNER JOIN "model_b_table" U1 ON (U0."foo_id" = U1."id") 
WHERE U1."bar" = 20

The Cast should use U0.id as the column selector.

ercpe commented 3 months ago

I've created a patch in https://github.com/etianen/django-watson/pull/309.

The Cast call will use a TextField as the output, although CharField (to cast into a varchar) ought to be enough. I've left do_string_cast in place. It's unused now, but someone may rely on it being present in the backend implementation.

It would be great to have this fix merged and released since it's blocking our search. Thanks!