Hi guys, I'm new to Supabase and I am having troub...
# help
z
Hi guys, I'm new to Supabase and I am having trouble to add a column to a public table that links to auth.users raw_user_meta_data. It is saying "There is no unique constrains matching given keys for 'users'". No RLS are set for now. I have a react app running, set OAuth with Google and created a public table called "posts" which already save in the table the registered user_uuid when posting. I don't get how to directly obtain user metadata when selecting posts in my app... Really appreciate if you could guide me on this 🙂 Thanks!
n
Hello @Zarckk! This thread has been automatically created from your message in #843999948717555735 a few seconds ago. We have already mentioned the @User so that they can see your message and help you as soon as possible! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ``...`` menu) and select "Leave Thread" to unsubscribe from future updates. Want to change the title? Use the ``/title`` command! We have solved your problem? Click the button below to archive it.
g
Can you show an example of your code? It is not clear exactly what you are trying to do with the meta data column.
n
Zarckk (2022-05-16)
z
Sorry for not being clear. Just a simple question can resolve what i'm trying to do. How do I fetch the posts with populated user 'name' (for example) ? As the user created the post, and its uuid is saved in a post
Copy code
if (req.method === 'POST') {
    const { message } = req.body;

    await supabaseClient.from('posts').insert([
      {
        content: message.content,
        user_id: message.userId,
      },
    ]);

    return res.status(201).json({});
  }
Copy code
if (req.method === 'GET') {
    let { data: messages } = await supabaseClient
      .from('posts')
      .select(
content, id, created_at, edited_at, user_id
Copy code
)
      .order('created_at', { ascending: false });

    return res.status(200).json({
      messages,
    });
  }
As an example, In my get method, I would like to get the name like this :
Copy code
let { data: messages } = await supabaseClient
      .from('posts')
      .select(
content, id, created_at, user_id, user ( name )
Copy code
)
      .order('created_at', { ascending: false });
g
You cannot access auth.users from api. So either you have to create a public table using a insert trigger function on auth.users, then you can use that table, or you need to use an rpc function.
z
Thank you for the documentation link. Ok I understand better why now
But is is possible to perform this through the Table editor/the UI? I don't reaaly get where should I do that in the application interface
Hm, yeah, even through the documentation, I still don't get how to perform all of this in the App. I understand that I need to create a public table, and somehow populate this table with the info I need, when a user is created. But it's not clear to me. I think I will just store the metadata I need in the post table when creating it. Thanks anyway for your help
g
Well, if you have name stored in a public profile table with uuid as the key, then you can have a foreign key reference to the profile table in your post table. That would allow profile (name) to be used like you were trying on users.
z
I don't have the name stored for now. I managed to go through some steps thanks to this video :

https://www.youtube.com/watch?v=0N6M5BBe9AEâ–¾

and what you just said :). Now I successfully add a profile row when a user is registered.
In my public profiles, my new row contains the user id
id
, that's good. But I don't know how to insert
raw_user_meta_data
which contains the user name. And I have to do something in the trigger functions, but I did not get (due to my bad english) how to proceed.
Copy code
begin
  insert into public.profiles(id)
  values(new.id);

  return new;
end;
Found it!! Thanks, you are the best! My function looks like this
Copy code
begin
  insert into public.profiles(id, name)
  values(new.id, new.raw_user_meta_data->>'name');

  return new;
end;
n
Create public profile to access metadata when user registers