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

    Jaeden

    10/07/2021, 11:23 AM
    Thanks, works!
  • j

    Jaeden

    10/07/2021, 12:52 PM
    I added a trigger to the auth.users table on insert. But author_id is still empty whilst it should contain the
    id
    from the auth.users table?
    Copy code
    -- create function for adding user to default watchlist
    CREATE OR REPLACE FUNCTION give_user_default_watchlist() RETURNS TRIGGER as
    $$
      BEGIN
        insert into public.watchlists(author_id) VALUES (new.id);
        return new;
      END
    $$ language plpgsql security definer;
    
    --Use the function when auth.users gets an insert
    drop trigger if exists default_user_watchlist on auth.users;
    create trigger default_user_watchlist after insert on auth.users execute procedure give_user_default_watchlist();
    j
    • 2
    • 6
  • j

    jason-lynx

    10/08/2021, 3:50 AM
    looks right, but are you trying to insert a new row or update the existing row that you have in your screenshot?
  • j

    Jaeden

    10/08/2021, 10:22 AM
    Insert a new row!
  • j

    jason-lynx

    10/08/2021, 11:02 AM
    insert trigger
  • j

    jonhelge

    10/08/2021, 11:40 AM
    Hi, I am connecting directly to the postgres database and after a few minutes of inactivity it seems like the connection is broken. Is this expected behaviour?
  • l

    laznic

    10/08/2021, 3:18 PM
    Can someone spot if I'm doing something wrong here? I'm trying to go through all the rows in my table and insert data to a different table. Inserting outside the loop works fine, however nothing happens here even though the functions results in success.
    Copy code
    create or replace function public.add_region_ranks() 
    returns void
    as $$
    declare
      player record;
    begin
      for player in select * from profiles
      loop
        insert into profiles_ratings (profile_id, region_id, game_mode_id) values (player.id, 1, 1);
      end loop;
    end;
    $$ language plpgsql security definer;
    s
    • 2
    • 4
  • s

    stibbs

    10/11/2021, 11:38 PM
    How do I make this sql sort using supabase-js?
    Copy code
    sql
    order by min != 0 desc, inserted_at desc
    I thought it would be something like
    Copy code
    js
    ...
    .order('min', { "!= 0", ascending: false })
    .order('inserted_at', { ascending: false });
  • s

    stibbs

    10/12/2021, 2:04 AM
    Alternatively, if I make all my
    min
    0 values null, is there a way I can do order by nulls last, but NOT also sort min values asc/desc?
  • s

    stibbs

    10/12/2021, 2:32 AM
    Is there a way to do two queries and
    union
    the results with the API?
  • m

    Mihai Andrei

    10/12/2021, 6:06 AM
    From what i know, you can write that query in a rpc
  • m

    Mihai Andrei

    10/12/2021, 6:06 AM
    And call that from the Api
  • s

    stibbs

    10/13/2021, 1:35 AM
    That was the answer, thanks!
  • s

    Sølve

    10/14/2021, 8:55 AM
    Hello! I am wondering if anyone has any suggestions on how to get a sort of "column-level" security for my table? I have a users-table (in the
    public
    schema), and want to allow users to change their own name, but not the id (which references
    auth.id
    ). I have the following policy:
    Copy code
    sql
    CREATE POLICY "Users may only view their own data"
            ON pub_users FOR SELECT using (
                auth.uid() = id
            )
    And need something similar for an update policy
    Copy code
    sql
    -- How do I restrict the UPDATE to only be allowed for the `name`-column?
    CREATE POLICY "Users may only update their own name"
    ON pub_users FOR UPDATE using (
       auth.uid() = id
    )
  • c

    chipilov

    10/14/2021, 10:04 AM
    In addition to RLS, Postgres also has a Privileges system which allows to control the access to columns. For example, you can grant only read access to a column but revoke updates. You can read more about it here: https://www.postgresql.org/docs/13/ddl-priv.html
  • c

    chipilov

    10/14/2021, 10:11 AM
    Another option is to move the pub_users table to a private schema (so it's NOT accessible via REST) and then allow reading it using a read-only view and allow updating it it through a stored procedure so you have control over what can be updated and what not
  • s

    Sølve

    10/14/2021, 10:13 AM
    Thanks for your feedback @User! I am looking mostly into the second option, of creating a view, but a writable view. Something like
    Copy code
    sql
    CREATE OR REPLACE VIEW editable_user AS
                SELECT u.name
            FROM pub_users u 
                WHERE auth.uid() = u.id
  • s

    Sølve

    10/14/2021, 10:15 AM
    This seems to do the trick and I am successfully writing to the view, which is then updating the underlying pub_users (which has RLS enabled). I am still experimenting around this to understand the implications this has, or if I am opening up some unwanted access
  • c

    chipilov

    10/14/2021, 10:27 AM
    This will work, as long as all you need to expose to the user for reading is the username. However, there are a couple of caveats to be aware of: 1) If your pub_users table is in the public schema (like the view) users will still have access to it via REST so the column will still be updatable through the table 2) You can update the view because it is simple enough to be considered by Postgres auto-updatable (see "Updatable Views" section here: https://www.postgresql.org/docs/13/sql-createview.html) - if you complicate the view, it might be updatable anymore 3) View are executed with the role of the user which DEFINED the view, NOT the role who is calling the view. This means, that you need to ensure to define the view with a role which does NOT bypass RLS (for example, the postgres and supabase_admin roles which come with the Supabase PostgreSQL bypass RLS). You can read a little more here: https://github.com/supabase/supabase/discussions/1501#discussioncomment-1439606 (or in Postgres docs) 4) Pros and cons of updatable views according to the folks at Hasura: https://hasura.io/blog/the-pros-and-cons-of-updatable-views/
  • s

    Sølve

    10/14/2021, 10:36 AM
    Thanks, these are some very good points! I am indeed keeping my
    pub_users
    in public, and it has RLS enabled with the following policies:
    Copy code
    sql
    CREATE POLICY "Users may only view their own data"
            ON pub_users FOR SELECT using (
                auth.uid() = id
            )
    And my admin-roles (also defined in the pub_users table)
    Copy code
    sql
    CREATE POLICY "Admin users have full access to user data"
                ON pub_users FOR ALL using (is_admin(auth.uid()))
    All my DB code is handled (migrated, rolled back, seeded) by knex, connecting with service key. I am now wondering if this means the view will be owned by the service/superadmin. I will certainly have to dig a bit deeper to see if this works in practice!
  • m

    mikebarkmin

    10/14/2021, 11:31 AM
    Did you create a service key? Or how did you manage your migration. I am currently using db-migrate and am not sure how to include for example buckets. The normal postgres user has no access to the table. I think that is not a good idea to give the normal postgres user access to this table. Did you create a new user for managing the migrations?
  • s

    Sølve

    10/14/2021, 12:09 PM
    @User I am using the service key to run migrations using Knex. I run migrations from local dev (against my local dev supabase) and from github workflow actions against my test and prod environments. In local dev I have the service key in a
    .env.local
    file, and in my GitHub workflows it is stored as a secret. I do not know if this will work for buckets, as my project currently does not use any buckets
  • m

    mikebarkmin

    10/14/2021, 12:13 PM
    Can you show the connection object you are setting up for knex?
  • s

    Sølve

    10/14/2021, 12:15 PM
    Sure, here is my knexfile:
  • s

    Sølve

    10/14/2021, 12:17 PM
    for my local dev env the connection string would be
    SUPABASE_CONNECTION_STRING=postgres://postgres:postgres@localhost:5432/postgres
  • s

    Sølve

    10/14/2021, 12:17 PM
    and then for the test/prod environments replace the pw and host with your app password/host
  • m

    mikebarkmin

    10/14/2021, 12:18 PM
    But then your are connecting to the database with the normal postgres user. Where does the service key comes into play?
  • s

    Sølve

    10/14/2021, 12:19 PM
    Yeah you're right, no service key involved here! Apologies, it's been a few months since I set this up, so I forgot 😬 Sorry, complete brain fart there. Of course, knex needs a db-connection, not a REST API
  • m

    mikebarkmin

    10/14/2021, 12:38 PM
    no problem 😉
  • k

    kennethcassel

    10/20/2021, 5:26 PM
    Anyone know how I can manually set a jwt for auth in sql? I want to run an explain command against a query but as an auth’d user so I can test RLS performance
    e
    • 2
    • 11
1...192021...52Latest