Hey guys, I've been trying to run this code in my ...
# help
r
Hey guys, I've been trying to run this code in my SQL editor and haven't gotten updates on my table to work. Can anyone help point out what's wrong?
Copy code
CREATE OR REPLACE FUNCTION public.test_fn(row_id bigint) RETURNS void AS $$
    UPDATE restaurants SET name = 'test'
    WHERE row_id = 36;
$$ language sql volatile;
n
Hello @rockinbinbin! This thread has been automatically created from your message in #843999948717555735 a ``few seconds ago``. Pinging @User so that they see this as well! 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.
r
I've already disabled RLS on this
restaurants
table so that I can focus on getting the update working.
Also, the UPDATE code works fine in the editor without the wrapping function, but I need to make it a function so that I can call it client side.
s
You need to add
BEGIN
and
END
around your function code I believe. I'd guess you're getting an error. First step to troubleshooting would be to
const { data, error } = await supabase.rpc("test_fn")
and
console.log({ error })
n
rockinbinbin (2022-03-25)
r
can you give me an example?
g
SQL language function won't work with Begin/End. Function seems fine I just ran it on a table I have. Can you run it from the sql window?
r
I've been running it in the SQL editor and it says success but the table column doesn't update
g
is row_id a bigint?
OHH you have variable named row_id
what is your column name
r
yeah but i'm not even using it
g
Is your column name row_id?
They both can't be same name... I used id for my row which I already have in my table.
r
column name is actually id that was a mistake! but it's still not updating :/
Copy code
CREATE OR REPLACE FUNCTION public.test_fn(row_id bigint) RETURNS void AS $$
    UPDATE restaurants SET name = 'test'
    WHERE restaurants.id = 36;
$$ language sql volatile;
g
It is odd you said the sql alone worked, even with the row_id column name? This works:
Copy code
CREATE OR REPLACE FUNCTION public.test_fn(row_id bigint) RETURNS void AS $$
    UPDATE messages SET message = 'test'
    WHERE id = 15;
$$ language sql volatile;
And it works just passing in the variable with id = row_id
Are your table and column names all lower case? is name text or varchar?
r
name is text
is that a problem?
g
No that is good
r
I basically am using this as a test to get my table to update, because what I actually want to do is use SQL to update my geometry column
but i'm still not able to get a basic test update working
g
So you do
select test_fn(1111)
and it does nothing? From sql editor?
r
would you be alright with video calling to figure this out quick?
g
About to eat dinner my time, so no. Post your table structure. RLS does not matter from the sql editor.
r
ok no prob, here it is
When I click "Run," the query executes successfully. But the name column does not update
I want to get updates working so that I can eventually run this from my client:
UPDATE restaurants SET geom = ST_SetSRID(ST_MakePoint(restaurants.lng, restaurants.lat), 4326);
(This works perfectly from SQL editor while it's not wrapped in a function).
g
So you see something like this when your run the function? Also did you ever have a function by the same name with perhaps no variable or a different one?
r
This is what I see when I run it
And definitely have been updating the function name repeatedly to avoid that issue
g
That is you setting up the function, not running it
r
Where can I run it?
g
like in my image at top
r
ok let me try it
okay, I've run it that way and still no change
Ah I've got it to work with the older fn name
!
test_fn
really odd, i don't know why...
g
Should also then work from rpc with no rls on.
Good luck
r
things are working now! thanks so much!
I'm actually still unable to get any logs on data or errors when calling this from the client side. Are there any deeper function logs I can investigate?
Got it all to work after updating RLS. Really appreciate your help here today. 🙏