Skip to content

Search for a subset by filtering on related model fields #291

@ranebo

Description

@ranebo

Hello,

Thanks for creating and maintaining this great package!

I've run into a situation where I would like to search a subset of models by filtering on a related model's field, like this:

watson.search("Search terms", models=(MyModel.objects.filter(related__is_public=True),))

When I try this, I get a django.db.utils.ProgrammingError: column reference "id" is ambiguous error.

Not sure if i'm missing a configuration, or if this isn't feasible at the moment. Would be great to know a way how to do this.

Thanks!

P.S. Here is the actual db error I'm working with, incase something else is going on:

ERROR:  column reference "id" is ambiguous at character 578
STATEMENT:  SELECT (ts_rank_cd(watson_searchentry.search_tsv, to_tsquery('pg_catalog.simple', '$$c$$:*'))) AS "watson_rank", "watson_searchentry"."id", "watson_searchentry"."engine_slug", "watson_searchentry"."content_type_id", "watson_searchentry"."object_id", "watson_searchentry"."object_id_int", "watson_searchentry"."title", "watson_searchentry"."description", "watson_searchentry"."content", "watson_searchentry"."url", "watson_searchentry"."meta_encoded" FROM "watson_searchentry" WHERE ("watson_searchentry"."engine_slug" = 'bread' AND "watson_searchentry"."object_id" IN (SELECT ("id"::text) AS "watson_pk_str" FROM "services_servicefirm" U0 INNER JOIN "professionals_company" U1 ON (U0."company_id" = U1."id") WHERE (U1."claimed_on" IS NOT NULL AND U1."deleted_on" IS NULL AND U1."is_public" = true AND U0."deleted_on" IS NULL AND U0."is_public" = true AND U0."membership_is_active" = true)) AND "watson_searchentry"."content_type_id" = 124 AND (search_tsv @@ to_tsquery('pg_catalog.simple', '$$c$$:*'))) ORDER BY "watson_rank" DESC  LIMIT 21

from

filters = dict(
    company__claimed_on__isnull=False,
    company__deleted_on=None,
    deleted_on=None,
    company__is_public=True,
    is_public=True,
    membership_is_active=True,
)

watson.search("Search terms", models=(ServiceFirm.objects.filter(**filters),))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions