Syntax error in postgress functions
Hi there, I'm very new to this whole database thing and thought that supabase seemed not so intimidating. I am however running into a problem. I'm following this tutorial https://www.youtube.com/watch?v=I6nnp9AINJk because just like he shows in the video I want to increment a value. I'm pretty sure I've followed the guide to a tee, yet supabase is giving me a syntax error and it's really not that helpful because it's just saying at or around variable name. Maybe one of the more advanced users could point out where the problem occurs 🙏
I've set the argument to be card_id.
The specific error is: Failed to create function: failed to create pg.functions: syntax error at or near "new_amount"
Supabase
YouTube
Call Postgres functions from JavaScript with RPC
For the introductory video on PostgreSQL functions, check out 👉 https://www.youtube.com/watch?v=MJZCCpCYEqk
Sometimes we have a collection of complex steps we need to perform in a particular sequence from the front-end. Maybe we want to execute an SQL statement that the supabase-js library does not yet support. These are both excellent use case...
40 Replies
You are missing a closing ; @JuicyBenjamin.
I recommend using a database tool instead of the dashboard UI if you are just learning SQL.
I personally use and love Datagrip.
Is it just the semi colon? and where do I put it?

Not sure if it is just the semi-colon, please add and confirm.
You need a semi colon after card_id to finish the statement at least.
Your end needs a semi colon too.
And I don't see that you are using $$ anywhere?
The function UI does not need $$.
So you may run into escaping issues unless the dashboard doesn't require that.
Ah, ok.
Well, now I'm at least getting a new error:
Failed to create function: failed to create pg.functions: syntax error at or near "return"
Put a semi colon after
end
as well.Yea I did, same result
why is it semi colon some places but not all places? xD
Semi colon is wherever the code block ends.
https://www.postgresql.org/docs/14/sql-syntax.html this is a quick read and will probably help a lot.
PostgreSQL Documentation
Chapter 4. SQL Syntax
Chapter 4. SQL Syntax Table of Contents 4.1. Lexical Structure 4.1.1. Identifiers and Key Words 4.1.2. Constants 4.1.3. Operators 4.1.4. Special Characters …
I'm rebuilding the function in one of my databases and then I can let you know what is missing, just give me a minute.
This creates in the database manually with no issues. All I did was add the semi colons.
What are you doing that throws this error?
Failed to create function: failed to create pg.functions: syntax error at or near "return"
returns should be int though...
Oh, you are correct.
That was my bad, I cut and paste some other code at the top.
dunno, that's all the code I have :/
What are you clicking or doing to see that error?
I'm trying to replicate on my side.
I'm just trying to save the function
But it wont let me
Can you show your code again? Also don't think it would error on save but did you declare a return value and confirm you are using thefunction UI versus sql window.
Good point, are you using "Add New Function" or the SQL query window?
This is the Add New Function.

create a new function, under database --> functions in supabase
yea that one
where it says void in the pix, do you have int?
I just created a new function fine in the UI for Create New Function.
no, should it be int?
Yes, so it may be checking on save.

That works for me with no errors.
which of the int types, just int 8 or?
Depends on the estimated size of the int.
I just did 8 for testing purposes. If you are using low dollar amounts, could probably go lower but to be safe, int8 should be fine.
You can also just do integer.
hmm, still giving me error at or near return
(Which is what I should have done but didn't see it immediately.)
You cut and paste the above code into the code box?
I can try, should it not have the "declare" ?
I just tested with declare and it works as well.
Wait, Discord is removing my first line of code.
declare
new_amount int;
begin
select amount
into new_amount
from public.collection
where id = card_id;
new_amount = new_amount + 1;
update public.collection
set amount = new_amount
where id = card_id;
return new_amount;
end;
Sorry about that mess, my code isn't pasting properly for some reason.
That last block is what I used and it works fine.
Can you send a screenshot of the function window please?
seems to work now, but it did change something. Fingers crossed the function will work as intended
Thanks for the help guys
Ah, ok, great!
Supabase is just a UI and a bunch of PostgreSQL features pre-installed. I highly recommend using a database tool such as Datagrip and learning pl/pgsql structures, that will make everything so much easier to work with.
(Supabase is fantastic, don't get me wrong, but working directly in the database for most of your work will be much easier than using the web UI.)
I'll look into it, thanks
I 2nd datagrip, but it is an free addon for my ide, not sure what it costs. There are other options out there too.
Datagrip is $10 / month.
There are also free tools like DBeaver.
TablePlus is really nice.