https://supabase.com/ logo
Join Discord
Powered by
# sql
  • d

    dipankarmaikap

    03/17/2022, 11:57 AM
    hello, how can i trigger the profiles table to be updated when user metadata updated in auth.users table.?
  • d

    dipankarmaikap

    03/17/2022, 11:58 AM
    Copy code
    -- inserts a row into public.users
    create OR REPLACE function public.handle_new_user() 
    returns trigger 
    language plpgsql 
    security definer set search_path = public
    as $$
    begin
      insert into public.profiles (id, raw_user_meta_data)
      values (new.id, new.raw_user_meta_data);
      return new;
    end;
    $$;
    
    -- trigger the function every time a user is created
    DROP TRIGGER IF EXISTS on_auth_user_created on auth.users;
    create trigger on_auth_user_created
      after insert on auth.users
      for each row execute procedure public.handle_new_user();
    g
    • 2
    • 9
  • d

    dipankarmaikap

    03/17/2022, 12:00 PM
    This is working, when i first signup but if i update the auth metadata its not updating the profile table.
    Copy code
    await supabase.auth.update({
          data: { account_deleted: new Date() },
        });
  • g

    garyaustin

    03/17/2022, 1:21 PM
    Update profile trigger
  • a

    ak4zh

    03/18/2022, 7:18 AM
    I have following schema in my supabase project: I want to add some constraints to ensure following: - for each record in
    transactions
    atleast 2 record will be inserted in
    journals
    - for each record in
    journals
    against a
    transaction_id
    the sum of amount for
    is_credit=true
    must be equal to
    is_credit=false
    otherwise all records roll back - I also do not want any record in
    transactions
    without any corresponding records in
    journals
    How to achieve above ? I will be using supabase.js to push new records.
    Copy code
    sql
    CREATE TABLE IF NOT EXISTS public.accounts (
      id uuid NOT NULL DEFAULT uuid_generate_v4(),
      created_at timestamp with time zone DEFAULT now(),
      name text COLLATE pg_catalog."default" NOT NULL,
      CONSTRAINT accounts_pkey PRIMARY KEY (id),
    );
    
    
    CREATE TABLE transactions (
      id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1 ),
      created_at timestamp with time zone DEFAULT now(),
      transaction_date date not null default current_date,
      description text NOT NULL,
      CONSTRAINT transactions_pkey PRIMARY KEY (id),
    );
    
    CREATE TABLE journals(
      id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1 ),
      transaction_id bigint NOT NULL,
      account_id uuid NOT NULL,
      amount NUMERIC(20, 2) NOT NULL,
      is_credit boolean NOT NULL,
      CONSTRAINT journals_pkey PRIMARY KEY (id),
      CONSTRAINT journals_transaction_id_fkey FOREIGN KEY (transaction_id)
        REFERENCES public.transactions (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
      CONSTRAINT journals_account_id_fkey FOREIGN KEY (account_id)
        REFERENCES public.accounts (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
    );
    • 1
    • 1
  • a

    ak4zh

    03/18/2022, 9:27 AM
    Account Book Keeping SQL
  • a

    AmusedGrape

    03/18/2022, 1:40 PM
    I'm getting this error:
    syntax error at or near "insert"
    Copy code
    sql
    create function commands_inc (x int, row_id text) 
    returns void as
    $$
      insert into command_logs (id, commands) values (row_id, x) on conflict do update
    $$ 
    language plpgsql volatile;
    what am i doing wrong? thanks!
    a
    • 2
    • 10
  • d

    d33pu

    03/21/2022, 2:27 AM
    what am I doing wrong here ?
    Copy code
    create or replace function latestOilFee()
    return array
    IS
    Begin
    select * from oil_tracker order by ts desc fetch first 1 row only;
    End;
    Error: syntax error at or near "return"
    s
    • 2
    • 3
  • s

    Scott P

    03/21/2022, 2:39 AM
    Syntax Error return array
  • t

    TMShader

    03/22/2022, 1:29 PM
    Is it possible to create an array of unique UUIDs column?
    c
    n
    • 3
    • 4
  • c

    chipilov

    03/22/2022, 1:40 PM
    Column with array of unique UUIDs
  • m

    MDobs

    03/22/2022, 2:43 PM
    what type would you add on a Function to return all rows of a table? meaning the result of running this
    SELECT * FROM users
    s
    • 2
    • 9
  • a

    AmusedGrape

    03/22/2022, 5:54 PM
    been trying to figure this out for a while now, how can i merge two JSON objects into one and add the values together? example:
    Copy code
    To insert:
    { "a": 1, "b": 2 }
    
    Already there:
    { "a": 2, "b": 3, "c": 1}
    
    What I want:
    { "a": 3, "b": 5, "c": 1 }
    what's the best way to do this with SQL?
    c
    s
    • 3
    • 13
  • t

    TMShader

    03/23/2022, 8:24 PM
    Is there a way to create a policy that allows the user to update a specific column (not the whole row) if their uuid is inside another column in the same row?
    g
    • 2
    • 5
  • g

    garyaustin

    03/23/2022, 8:37 PM
    specific column policy
  • s

    sseppola

    03/24/2022, 8:23 AM
    I cannot seem to access the auth schema from a postgres function:
    Copy code
    CREATE OR REPLACE FUNCTION public.set_username(new_username text)
        RETURNS public.profile
        LANGUAGE 'plpgsql'
        SECURITY INVOKER
        SET SEARCH_PATH = public, auth
    AS
    $$
    declare
        updatedProfile public.profile;
    begin
        insert into public.profile(id, username)
        select auth.uid(), new_username
        returning * into updatedProfile;
    
        return updatedProfile;
    end
    $$;
    I get "permission denied for schema auth", but I thought with
    security invoker
    and setting the
    search_path
    auth should be available. It's called using the supabase.rpc function. Fyi, this is a contrived example of what I'm trying to do. Edit:
    SECURITY DEFINER
    does the trick, so I guess the invoker does not and should not have access to auth.uid?
  • g

    garyaustin

    03/24/2022, 1:44 PM
    SECURITY INVOKER runs with the level of the user. General API users calling RPC would not access to auth.
  • s

    sseppola

    03/24/2022, 2:55 PM
    This is what I want, so I ended up duplicating auth.uid() into the public schema in order to keep the auth schema isolated from users
    g
    • 2
    • 10
  • t

    truby

    03/26/2022, 7:40 AM
    What's the best work to handle a else if statement in a policy? This is what i'm trying...
    Copy code
    (if 
       auth.role() = 'authenticated'
       and 
       is_draft = false
     then
       true;
    elsif
       auth.uid() = user_id 
    then
       true;
    else
      false;
    end if;
    )
    a
    • 2
    • 4
  • a

    ak4zh

    03/26/2022, 7:57 AM
    Copy code
    sql
    CREATE FUNCTION node_cte(node_ids int[], org_id uuid)
    ...
    WHERE table.id IN node_ids
    How can I use the function arg to find all rows whose id is in the arg array. The above gives array at or near IN
    s
    g
    • 3
    • 5
  • s

    Scott P

    03/26/2022, 4:33 PM
    SQL includes
  • k

    Kirdes

    03/29/2022, 6:37 PM
    Hi. I'm quit new to sql so sorry if its a noob question but I need to increment some field, I'm using this and it works great
    Copy code
    sql
    create function increment (row_id int) 
    returns void as
    $$
      update "Song" 
      set listening = listening + 1
      where id = row_id;
    $$ 
    language sql volatile;
    So this is working only for the listening column, is it possible to dynamically set the column ? I have an other column that I need to increment. or I need to make an other function ? Thx
  • f

    fernandolguevara

    03/29/2022, 7:00 PM
    @User try with
    EXECUTE
  • f

    fernandolguevara

    03/29/2022, 7:01 PM
    Copy code
    EXECUTE format('SELECT * from result where id=%s', '"42"');
  • k

    Kirdes

    03/29/2022, 7:29 PM
    not sure how to do that tbh, I'll try
  • k

    kbanta11

    03/31/2022, 12:32 AM
    is it possible to set a database trigger to run a function when a user first verifies (using phone OTP or magic link)? I need to update some other values once users are verified
    g
    t
    • 3
    • 27
  • g

    garyaustin

    03/31/2022, 12:45 AM
    Verify User Trigger
  • n

    NARCISO

    04/01/2022, 2:22 PM
    Hi everyone, I have a bit of a complex problem that I don't know what's the best way to solve: I have the following
    Message
    table:
    Copy code
    CREATE TABLE IF NOT EXISTS public.message
    (
        id uuid NOT NULL,
        sender_user_id uuid NOT NULL,
        recipient_user_id uuid NOT NULL,
        conversation_id uuid NOT NULL,
        content text COLLATE pg_catalog."default" NOT NULL,
        read boolean DEFAULT false,
        message_warning boolean DEFAULT false,
        CONSTRAINT message_pkey PRIMARY KEY (id),
        CONSTRAINT message_conversation_id_fkey FOREIGN KEY (conversation_id)
            REFERENCES public.conversation (id) MATCH SIMPLE
            ON UPDATE CASCADE
            ON DELETE RESTRICT,
        CONSTRAINT message_sender_user_id_fkey FOREIGN KEY (sender_user_id)
            REFERENCES public."user" (id) MATCH SIMPLE
            ON UPDATE CASCADE
            ON DELETE RESTRICT,
    )
    The table is pretty simple but I have some the need of some
    "custom" Column Level Security
    . For example, I would like to let the user, through a
    RLS Policy
    , to only UPDATE the
    content
    and the
    read
    column. As far as I know this is not possible through a
    RLS Policy
    . Is there any other way to do this? or what's the best way to do this? maybe without using a
    db function
    ? Thanks!
    g
    s
    • 3
    • 4
  • g

    garyaustin

    04/01/2022, 2:59 PM
    Column Security
  • b

    Boni

    04/02/2022, 12:50 AM
    Hello, I created a table by importing data from a csv , this csv already has a ID column which i want to use as primary key and auto increment. I imported the db using supabase dashboard and then set the ID column to primary key and set checked the
    is identity
    . When inserting new rows the auto increment starts at 1. I tried setting the auto increment to begin at the end of the current max value of the ID following this github issue https://github.com/supabase/supabase/issues/1804 but when i try to access the sequence using
    SELECT PG_GET_SERIAL_SEQUENCE('mytable', 'id');
    i get
    relation "mytable" does not exist
    . Any idea how i can add the sequence to my table ? Thanks
1...373839...52Latest