Pragy
11/05/2021, 6:24 PMnew.raw_user_meta_data->>'avatar_url'
- new.raw_user_meta_data->>'full_name'
- uuid_generate_v4()
- uuid_generate_v4()::text
weirdly, random()::text
works though
any way to generate the same random value for 2 columns when inserting a value via sql?silentworks
11/05/2021, 6:35 PMsilentworks
11/05/2021, 6:37 PMPragy
11/05/2021, 6:48 PMsql
create schema if not exists mindwiki;
create table if not exists mindwiki.profiles
(
user_id uuid primary key references auth.users default auth.uid(),
uid uuid not null unique default uuid_generate_v4(),
created_at timestamp with time zone default now() not null,
updated_at timestamp with time zone not null default now(),
email citext not null unique default auth.email(),
is_public boolean default true,
username citext unique check (char_length(username) >= 3),
full_name text,
avatar_url text
);
No RLS was enabled
trigger
sql
create or replace function public.handle_new_user()
returns trigger as
$$
begin
insert into mindwiki.profiles (username, full_name, avatar_url)
values (null, new.raw_user_meta_data->>'full_name', new.raw_user_meta_data->>'avatar_url');
return new;
end;
$$ language plpgsql security definer;
create trigger on_auth_user_created
after insert
on auth.users
for each row
execute procedure public.handle_new_user();
After this, signup/magic-link gives the error: Inviting user failed: Database error saving new user
silentworks
11/05/2021, 6:49 PMPragy
11/05/2021, 6:51 PMnull
to 'trial'
gets the same error.
The current auth.users table and mindwiki.profiles table have no entriesPragy
11/05/2021, 6:51 PMsql
create or replace function public.handle_new_user()
returns trigger as
$$
begin
insert into mindwiki.profiles (username, full_name, avatar_url)
values ('trial', new.raw_user_meta_data->>'full_name', new.raw_user_meta_data->>'avatar_url');
return new;
end;
$$ language plpgsql security definer;
silentworks
11/05/2021, 6:53 PMmindwiki
schema, it doesn't know about it by default, it only knows of the public
schemasilentworks
11/05/2021, 6:53 PMset search_path = public, mindwiki
Pragy
11/05/2021, 6:56 PMsql
create or replace function public.handle_new_user()
returns trigger
language plpgsql
security definer set search_path = public, mindwiki
as
$$
begin
insert into mindwiki.profiles (username, full_name, avatar_url)
values ('trial', new.raw_user_meta_data ->> 'full_name', new.raw_user_meta_data ->> 'avatar_url');
return new;
end;
$$;
Tried both search_path = public, mindwiki
and search_path = mindwiki, public
silentworks
11/05/2021, 6:57 PMsilentworks
11/05/2021, 6:58 PMsilentworks
11/05/2021, 7:00 PMsql
user_id uuid primary key references auth.users default auth.uid(),
It might not be working as expected, because the auth.uid()
is probably null at the time of creation as I don't think it's a dynamic function when used in creating the table schemaPragy
11/05/2021, 7:01 PMid
to the users.
Someone here told me to create a new schema and put all the tables in there, and then create views in the public schema 😅
I just tried to put the table in the public schema but that didn't work either
sql
create table if not exists public.profiles
(
user_id uuid primary key references auth.users default auth.uid(),
uid uuid not null unique default uuid_generate_v4(),
created_at timestamp with time zone default now() not null,
updated_at timestamp with time zone not null default now(),
email citext not null unique default auth.email(),
is_public boolean default true,
username citext unique check (char_length(username) >= 3),
full_name text,
avatar_url text
);
create or replace function public.handle_new_user()
returns trigger
language plpgsql
security definer set search_path = public
as
$$
begin
insert into profiles (username, full_name, avatar_url)
values ('trial', new.raw_user_meta_data ->> 'full_name', new.raw_user_meta_data ->> 'avatar_url');
return new;
end;
$$;
create trigger on_auth_user_created
after insert
on auth.users
for each row
execute procedure public.handle_new_user();
silentworks
11/05/2021, 7:02 PMnew.raw_user...
before the arrow and after, is that how it is when you create the function?silentworks
11/05/2021, 7:06 PMPragy
11/05/2021, 7:07 PMdefault auth.uid()
, default auth.email()
and removing the spaces after & before the arrow operators worked!
My IDE was automatically adding those spaces 😢
I had tested the auth.uid()
via the set role authenticated;
method and it worked there.Pragy
11/05/2021, 7:07 PMsilentworks
11/05/2021, 7:10 PMPragy
11/05/2021, 7:12 PM