jar
04/12/2022, 6:05 AM-- declare
-- newusername text := generate_available_username();
-- newusername text := uuid_generate_v4();
begin
insert into public.profiles(username)
values(uuid_generate_v4());
return new;
end
or
begin
new.username := uuid_generate_v4();
return new;
end
Needle
04/12/2022, 6:05 AM/title
command!
We have solved your problem?
Click the button below to archive it.garyaustin
04/12/2022, 1:24 PMNeedle
04/12/2022, 1:24 PMjar
04/12/2022, 1:30 PMgaryaustin
04/12/2022, 1:32 PMjar
04/12/2022, 1:40 PMgaryaustin
04/12/2022, 1:50 PMraise log 'message' || variable
like you use public.profiles you might need extensions.function() if that is where they are.
You set search path like in the example of an auth trigger function: https://supabase.com/docs/guides/auth/managing-user-data#advanced-techniques You might need to add ,extensions to it.jar
04/12/2022, 1:56 PMcreate or replace function generate_available_username() returns text as $$
declare
count_of_adjectives int;
count_of_nouns int;
rand_num_adjective int := 0;
rand_num_noun int := 0;
chosen_adjective text;
chosen_noun text;
new_username text := uuid_generate_v4();
try_new_username text;
begin
select count(*) from adjectives into count_of_adjectives;
select count(*) from nouns into count_of_nouns;
FOR i IN 1..10 LOOP
rand_num_adjective = random_between(1,count_of_adjectives);
rand_num_noun = random_between(1,count_of_nouns);
select text from adjectives where id = rand_num_adjective into chosen_adjective;
select text from nouns where id = rand_num_noun into chosen_noun;
if not (coalesce(chosen_adjective, '') = '' or coalesce(chosen_noun, '') = '') then
try_new_username = chosen_adjective || '-' || chosen_noun;
IF NOT EXISTS (select 1 from profiles where username = try_new_username) THEN
return try_new_username;
END IF;
end if;
END LOOP;
return new_username;
end;
$$ language plpgsql
garyaustin
04/12/2022, 3:38 PMjar
04/12/2022, 4:05 PMgaryaustin
04/12/2022, 4:31 PMjar
04/12/2022, 4:37 PMgaryaustin
04/12/2022, 4:44 PMjar
04/12/2022, 4:53 PMgaryaustin
04/12/2022, 4:56 PM