PricingBlog

Supabase - store UUID of sponsor during Signup

  • the_guy01-1207993727667281990

    the_guy

    1 year ago

    I create an affiliate system.
    Backend is Supabase.

    First I get a UUID from auth.users.id
    I want to store this as a sponsor_id into a public table called "affilaite_tree"

    I used the user management tutorial to setup the trigger and function.
    When I just insert the new.id into affiliate_tree everything works as expected.

    When I want to store the sponsor_id in the function as well, I get an error stating that the sponsor_id is type text and not type uuid.
    How can I solve this error to store the sponsor_id as UUID?

    In the pictures you see how the payload object is structured.
    The sponsor_id is currently type "string".
    I also added a screenshot of the error message.
    1207993728032047124-Screenshot_2024-02-16_at_14.14.34.png
    1207993728375984138-Screenshot_2024-02-16_at_14.14.46.png
    1207993728732631050-Screenshot_2024-02-16_at_14.14.56.png
    1
  • the_guy01-1208340493625200690

    the_guy

    1 year ago

    I was able to fix it by casting the variable from text to UUID inside my Supabase function:

    Previous Function Code:
    Here, the sponsor_id was of type text
    insert into public.affiliate_tree (id, sponsor_id)
    values (new.id, new.raw_user_meta_data->>'sponsor_id');
    return new;
    end;


    Working Function Code:
    Here, the sponsor_id is now casted to type UUID
    insert into public.affiliate_tree (id, sponsor_id)
    values (new.id, (new.raw_user_meta_data->>'sponsor_id')::UUID);
    return new;
    end;