Skip to content

You might need to add explicit type casts in subqueries #538

@hamidrabedi

Description

@hamidrabedi

So I'm trying to do some aggregations and when I try to do sth like this:

actions_filter = Action.objects.filter(
    target_object_id=OuterRef("pk"),
    verb=ContentActivityType.VIEW_CONTENT,
)
view_subquery = Subquery(
    actions_filter.values("target_object_id")
    .annotate(views_count=Count("actor_object_id", distinct=True))
    .values("views_count")[:1]
)

Content.objects.annotate(number_of_views=Coalesce(view_subquery, 0))

I get error:

django.db.utils.ProgrammingError: operator does not exist: character varying = bigint
LINE 1: ... "actstream_action" U0 WHERE U0."actor_object_id" = ("accoun...
                                                             ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

And i have to cast the type of my ids in string each time:

target_object_id=Cast(OuterRef("pk"), output_field=CharField()),

Is there any way to fix this?
Why can't we change the *_object_id to integers?

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