https://supabase.com/ logo
RPC function issue with UPDATE
# help
s
RPC function issue with UPDATE
n
Hello @STILLWATER;! This thread has been automatically created from your message in #843999948717555735 a few seconds ago. We have already mentioned the @User so that they can see your message and help you as soon as possible! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ``...`` menu) and select "Leave Thread" to unsubscribe from future updates. Want to change the title? Use the ``/title`` command! We have solved your problem? Click the button below to archive it.
s
Hey So i was creating this function to update value using rpc in supabase
Copy code
sql
create function update_story_count(uid uuid, change_value int)
    returns void
    language plpgsql
as
$$
BEGIN
         (
            UPDATE "Users" U
              SET stories_count = stories_count + change_value
              WHERE U.id=uid
            );
END
$$;
But it says
all of my other function are like this, just it starts with
Copy code
return query (...)
but that was causing error as the function was not returning anything
so i removed return query
i was referencing this
any help?
tried removing brackets etc every permutation possible
only change is on the discussion they using sql volatile but mine is plpgsql is that an issue?
idk whats sql volatile so didnt changed that
s
You can just write it as
sql stable
there is no need for
plpsql
since you aren't using any of its features in this query. But your existing code should work too by just removing the brackets. I tested this and it worked for me.
Copy code
sql
create function update_story_count(uid uuid, change_value int)
    returns void
    language plpgsql
as
$$
BEGIN
    UPDATE "Users" U
    SET stories_count = stories_count + change_value
    WHERE U.id = uid;
END
$$;
s
HOLY HOW!
also
s
You only get 1 question per go.
s
got it thanks
s
I'm joking 🤣
s
bruh
s
Hahahaha
s
🔪
can i select the updated data from same query
s
Sorry I thought you would have caught on to that
s
i checked online solutions
s
Yeah you can
s
those online solution didnt work
s
So you would need to change the return type and then run a select query after
Let me test out a solution and then get back to you on here
s
thanks a lot
s
I haven't tested this with the
.rpc
call but it works inside the SQL editor
Copy code
sql
create or replace function update_story_count(uid uuid, change_value int)
    returns TABLE (id uuid, stories_count int)
as
$$
    UPDATE "Users" U
    SET stories_count = stories_count + change_value
    WHERE U.id = uid
    RETURNING U.id, U.stories_count; -- tells it which data to return
$$
language sql; -- changed to sql instead of plpsql
s
does it not work on plpgsql?
s
It's not using any
plpgsql
specific feature in here
s
just for uniformity 😅
"success": false, "log": "column reference \"stories_count\" is ambiguous"
:((
ambiguity in SET
s
You changed it to
plpgsql
right?
s
yeah
s
haha
s
ill change to sql?
damn it makes a diff?!?
s
Indeed it does
s
damn bro ur a lifesaver
thanks!
also why BEGIN END doesnt work here?
is that syntax for plpgsql specifically?
s
s
thanks