https://supabase.com/ logo
#help
Title
# help
a

Adamo

08/16/2021, 2:41 PM
when you call
Copy code
typescript
const { user, error } = await supabase.auth.signUp({ email, password });
How would I go about attaching a username to the newly created user? I'm pretty confused about how the API works. I have a 'profiles' table that was created from the
User Management
starter, and I tried calling this after creating the user, which doesn't seem to work.
Copy code
typescript
await supabase.from('profiles').update({ username }).eq('id', user?.id);
s

Scott P

08/16/2021, 2:47 PM
I'd first check what
username
and
user?.id
are (just console.log them to see). If I was to guess, I'd say that
user?.id
is undefined or null, but I can't be 100% sure.
a

Adamo

08/16/2021, 2:50 PM
it comes back correctly
s

Scott P

08/16/2021, 3:25 PM
If you do this:
const {error, data} = await supabase.from('profiles').update({ username }).eq('id', user?.id);
What do you get returned?
a

Adamo

08/16/2021, 4:19 PM
I don't get an error, and the data object has a user_metadata field with the username I submitted from the form like expected.
that's what I'm finding so weird about this.
There should be a user shown here with an
id
and
username
set right?
under authentication/users I have a few random ones I created listed
my bad..
s

Scott P

08/16/2021, 4:25 PM
Ah, OK, I see what's happening. You're asking it to
update
a row which doesn't exist. The row needs to exist before update will work. The easiest way to handle this automatically is to create a trigger to create the record in the
public.profiles
table. This example (https://github.com/supabase/supabase/discussions/306#discussioncomment-138424) should help, though you'll need to replace
public.users
with
public.profiles
.
a

Adamo

08/16/2021, 4:25 PM
I made an error here
I was using
auth.update
switched it back to the code snippet you shared
and I got a 404
id
not found
which makes sense because there is nothing in
profiles
So where exactly does that code go?
s

Scott P

08/16/2021, 4:31 PM
Just run it from the SQL editor
a

Adamo

08/16/2021, 4:32 PM
So anytime a new row gets added to auth.users, it will trigger this function to run
which copies over the id/email into public.profiles
Okay that didn't work.
I think I'm suppose to use
.upsert
instead of
.update
okay signing up the user, and calling
.upsert
on the profile after with username/userId worked
Copy code
typescript
const { user, error } = await supabase.auth.signUp({ email, password });

if (error) throw error;

const updates = {
  id: user?.id,
  username,
};

let { error: updateError, data } = await supabase
  .from('profiles')
  .upsert(updates, {
    returning: 'minimal',
   });

if (updateError) throw updateError;