I am creating an rpc function to get the number of likes for each post for a user, so I have created a function that takes userId as an argument, which is the uuid of the user that is in the session. But when I call the function on the front it returns me.
If a new function was created in the database with this name and parameters, try reloading the schema cache.
The function:
create function get_number_of_posts_by_user(userId uuid)
returns integer
as $$
SELECT
count(pl.id)
FROM
auth.users au
join posts p on p.user_id = au.id
join post_likes pl on pl.post_id = p.id
where au.id = userId
$$ language sql;
You need to group by post to get there.
Call:
Major points:
You don't need to involve the table
usersat all. Filter byposts.user_iddirectly. Cheaper.count(*)>>count(pl.id)in this case. A bit cheaper, too. count(*) has a separate implementation in Postgres.count()returnsbigint, notinteger. Match whatRETURNSdeclares one way or the other.Avoid naming conflicts between function parameters and table columns. Prefixing
_for parameters (and never using the same for column names) is one common convention.And table-qualify column names. In this case to avoid a conflict between the
OUTparameterpost_id(also visible in the query) andpost_likes.post_id.When counting the number of likes, don't call your function "get_number_of_posts...".
Your original issue may have been a XY problem that goes away with a proper function definition.
Addressing the title
If you actually need to reload the schema cache, use:
The manual:
I have never had a need for this myself, yet. Typically, the problem lies elsewhere.