Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hamidrabedi opened this issue Feb 29, 2024 · 0 comments
Open

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

hamidrabedi opened this issue Feb 29, 2024 · 0 comments

Comments

@hamidrabedi
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant