waptik
09/09/2021, 5:09 PMScott P
09/09/2021, 5:12 PMwaptik
09/09/2021, 5:13 PMnetAmount
. Here's what i did:
declare
total integer;
begin
select count(netAmount)
into total
from donations
where beneficiaryId = id;
return total;
end;
waptik
09/09/2021, 5:14 PMget_film_count
in https://www.postgresqltutorial.com/postgresql-create-function/?Scott P
09/09/2021, 5:19 PMsql
CREATE OR REPLACE FUNCTION public.get_net_amount_total(in_id uuid)
RETURNS money
AS $BODY$
BEGIN
RETURN QUERY
SELECT count(netAmount) AS total
FROM public.donations
WHERE id = in_id;
END;
$BODY$ language 'plpgsql';
When you call it from Supabase, you'd call:
supabase.rpc("get_net_amount_total", {in_id: "some_uuid"})
I'm assuming that id
is a UUID - if not, change the data type in the arguments of the function.
money
is the data type that's returned (https://www.postgresql.org/docs/9.1/datatype-money.html)waptik
09/09/2021, 5:25 PMid
is a UUID. But the code i showed above kinda work but i get this error column "netamount" does not exist
. I have a netAmount
column in my table.
Here's how i call it
const { data, error } = await supabase.rpc("get_balance", { id });
Scott P
09/09/2021, 5:27 PMnetAmount
in the query (e.g. "netAmount"
)
- Rename the column to net_amount
The first option is easier, but the second option would be my recommendation. Postgres is easier to work with if you use all-lowercase, or snake-case for column nameswaptik
09/09/2021, 5:30 PMbeneficiaryId
.
Now got this error: column reference "id" is ambiguous
waptik
09/09/2021, 5:30 PMScott P
09/09/2021, 5:31 PMAS
, like this:
sql
CREATE OR REPLACE FUNCTION public.get_net_amount_total(in_id uuid)
RETURNS money
AS $BODY$
BEGIN
RETURN QUERY
SELECT count(netAmount) AS total
FROM public.donations AS donations
WHERE donations.id = in_id;
END;
$BODY$ language 'plpgsql';
waptik
09/09/2021, 5:37 PMwaptik
09/09/2021, 5:37 PMwaptik
09/09/2021, 5:38 PMsql
CREATE OR REPLACE FUNCTION public.get_balance(id uuid)
RETURNS money
AS $BODY$
BEGIN
RETURN QUERY
SELECT count("netAmount") AS total
FROM public.donations as donations
WHERE donations."beneficiaryId" = id;
END;
$BODY$ language 'plpgsql';
Scott P
09/09/2021, 5:39 PMwaptik
09/09/2021, 5:40 PMwaptik
09/09/2021, 5:41 PMcannot use RETURN QUERY in a non-SETOF function
waptik
09/09/2021, 5:57 PMsql
CREATE OR REPLACE FUNCTION public.get_balance(id uuid)
RETURNS integer
AS $BODY$
BEGIN
SELECT SUM("netAmount") AS total
FROM public.donations as donations
WHERE donations."beneficiaryId" = id;
RETURN total;
END;
$BODY$ language 'plpgsql';
waptik
09/09/2021, 5:57 PMcolumn reference "id" is ambiguous
Scott P
09/09/2021, 6:01 PMid
column on the same table, it might be trying to match against that.
Whenever you're defining arguments for a function, it's best to make sure they don't share the name with any column - prefixing them with in_
is how I prevent that from happening.waptik
09/09/2021, 6:09 PMsql
DECLARE
total integer;
BEGIN
SELECT SUM("netAmount")
INTO total
FROM public.donations as donations
WHERE donations."beneficiaryId" = userid; //params is now `userid`
RETURN total;
END;
Thanks for your help and time