justquick / django-activity-stream

Generate generic activity streams from the actions on your site. Users can follow any actors' activities for personalized streams.
http://django-activity-stream.rtfd.io/en/latest/
BSD 3-Clause "New" or "Revised" License
2.36k stars 483 forks source link

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

Open hamidrabedi opened 6 months ago

hamidrabedi commented 6 months ago

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?