Nin
06/17/2022, 11:46 PMNeedle
06/17/2022, 11:46 PMgaryaustin
06/17/2022, 11:52 PMNin
06/17/2022, 11:53 PMNin
06/17/2022, 11:54 PMNin
06/17/2022, 11:56 PMgaryaustin
06/17/2022, 11:57 PMNin
06/17/2022, 11:57 PMNin
06/17/2022, 11:57 PMNin
06/18/2022, 12:00 AMgaryaustin
06/18/2022, 12:00 AMNin
06/18/2022, 12:00 AMgaryaustin
06/18/2022, 12:01 AMNin
06/18/2022, 12:01 AMNin
06/18/2022, 12:01 AMgaryaustin
06/18/2022, 12:01 AMgaryaustin
06/18/2022, 12:02 AMNin
06/18/2022, 12:03 AMgaryaustin
06/18/2022, 12:03 AMgaryaustin
06/18/2022, 12:04 AMNin
06/18/2022, 12:04 AMgaryaustin
06/18/2022, 12:04 AMNin
06/18/2022, 12:04 AMgaryaustin
06/18/2022, 12:04 AMNin
06/18/2022, 12:04 AMgaryaustin
06/18/2022, 12:08 AMNin
06/18/2022, 12:08 AMgaryaustin
06/18/2022, 12:09 AMNin
06/18/2022, 12:10 AMNin
06/18/2022, 12:11 AMgaryaustin
06/18/2022, 12:11 AMBEGIN
IF NOT EXISTS(SELECT 1 FROM cart WHERE user_id = auth.uid()) THEN
INSERT INTO cart (user_id, total) VALUES (auth.uid(), 0)
RETURNING id INTO new_cart_id;
INSERT INTO cart_item (cart_id, product_id, user_id, quantity)
VALUES (
new_cart_id,
new_product_id,
auth.uid(),
new_quantity
) RETURNING * INTO new_cart_item
Nin
06/18/2022, 12:12 AMgaryaustin
06/18/2022, 12:12 AMNin
06/18/2022, 12:12 AMNeedle
06/18/2022, 12:13 AMNin
06/18/2022, 12:22 AMdeclare tenant_id public.tenants.tenant_id%TYPE;
begin
insert into public.tenants (tenant_name) values (new.raw_user_meta_data->>'tenant_name');
returning tenant_id INTO tenant_id;
INSERT INTO public.users (user_id, tenant_id, user_email)
VALUES (
new.id,
tenant_id,
new.email
) return new;
end;
This gives me the error:
Failed to create function: failed to update pg.functions with the given ID: syntax error at or near "returning"
Whereas without the declare it doesn't know what tenant_id
is.garyaustin
06/18/2022, 12:26 AMNin
06/18/2022, 12:27 AMNin
06/18/2022, 12:32 AMNin
06/18/2022, 12:35 AMdeclare tenant public.tenants.tenant_id%TYPE;
begin
insert into public.tenants (tenant_name) values (new.raw_user_meta_data->>'tenant_name')
returning tenant_id INTO tenant;
INSERT INTO public.users (user_id, tenant_id, user_email)
VALUES (
new.id,
tenant,
new.email
); return new;
end;
The query that did it.Needle
06/18/2022, 12:35 AMNin
06/18/2022, 12:36 AMNeedle
06/18/2022, 12:36 AM