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

    Albert [tox/cis]

    02/24/2022, 11:27 AM
    I'm trying to write a function which simply updates a value in the database if called
    Copy code
    sql
    create or replace function foo(id uuid) returns boolean as $$
      begin
        update tablename set bar='test';
        return true;
      end
    $$ language plpgsql;
    This returns
    true
    but the table rows are not updated. When commenting out everything except the update query, it updates as expected. Why?
    s
    • 2
    • 9
  • t

    tmw

    02/26/2022, 9:36 PM
    Can't tell if this is a bug... I'm trying to create a unique case insensitive column using citext. I've installed the extension, created the column, however when I try to make it unique I get "could not create unique index" - afaik this should be possible with citext
  • t

    tmw

    02/26/2022, 9:36 PM
    This happens using the gui or with the following
    Copy code
    ALTER TABLE table_name
    ADD CONSTRAINT column_name_unique UNIQUE (column_name);
  • t

    tmw

    02/26/2022, 9:37 PM
    Would love any help/ suggestions! Would rather not use lower and ilike queries
  • g

    garyaustin

    02/26/2022, 10:40 PM
    Do you have any data in the column? Specifically empty strings? Citext does work with unique constraint.
  • u

    2old4this

    02/27/2022, 8:03 PM
    trying to create a fkey to auth.users.email_change_token_new. but getting an error--there is no unique constraint matching given keys for referenced table "users", this happens when Is Unique checkbox is either checked on unchecked, Any ideas?
  • g

    garyaustin

    02/27/2022, 8:11 PM
    The error is on auth.users column not being unique (not your link you are creating), and you really should not change the auth schema.
  • g

    garyaustin

    02/27/2022, 8:14 PM
    Those are unique columns, so you can have foreign links
  • u

    2old4this

    02/27/2022, 8:14 PM
    not looking to change it, just looking to create a relation to my profiles table, like i did with auth.id, and auth.email
  • u

    2old4this

    02/27/2022, 8:15 PM
    can i query the auth.users able directly then?
    g
    • 2
    • 5
  • m

    mangysaurus

    03/02/2022, 12:29 AM
    Pretty new to this; What is the equivalent of the .valid? method in Ruby for Postgres? My goal is to utilize the .rpc method to evaluate if a record exists and return a boolean
    s
    • 2
    • 2
  • d

    DeadlyDev

    03/02/2022, 6:46 PM
    I have a tasks table, a projects table and a projects_tasks table. Pretty simple relationship stuff. However I have a need that I believe is outside my sql knowledge. I need a unique incrementing value (just a simple int) per project that goes up every time a task is added, but only relative to the previous task with the same project_id. So it could be like this...
    Copy code
    id   task_id   project_id   project_task_number
    ------------------------------------------------
    1    15        42           1
    2    16        42           2
    3    17        44           1
    4    18        44           2
    5    19        45           1
    6    20        42           3
    7    21        42           4
    What is the best way to go about this?
    g
    • 2
    • 1
  • g

    garyaustin

    03/02/2022, 7:33 PM
    count of tasks by project id
  • a

    asleepingpanda

    03/03/2022, 5:28 PM
    Does anybody know how to get this set up on Supabase? In the Supabase SQL editor, I'm getting an error when running this script Script I want to run: https://supabase-sql.vercel.app/youtube-like-short-id Error:
    type "shortkey" does not exist
  • s

    Scott P

    03/03/2022, 5:34 PM
    Likely because the
    pg_shortkey
    extension isn't installed in the database, meaning the types it generates don't exist. There's a thread for extension suggestions available at https://github.com/supabase/supabase/discussions/679
  • a

    asleepingpanda

    03/03/2022, 5:38 PM
    Oh ok I see. So this extension isn't available on supabase? How did the user who posted that script get it to work on supase?
  • s

    Scott P

    03/03/2022, 5:43 PM
    Actually, I've just re-read this. Did you run the script at https://github.com/turbo/pg-shortkey/blob/master/pg-shortkey.sql (that's the source of the shortkey type)? It only requires the pg-crypto extension, which is already installed in Supabase (Reply in the thread so we can avoid cluttering up the main chat)
    a
    • 2
    • 6
  • f

    Facu

    03/03/2022, 7:13 PM
    Hey, im with a question. Theres a function to get the current timestamp in miliseconds ? Like
    'uuid_generate_v4()'
  • s

    Scott P

    03/03/2022, 7:15 PM
    https://dba.stackexchange.com/a/2759
  • f

    Facu

    03/03/2022, 7:25 PM
    nicee it gives me a str like "1646335448040.042000". How can i convert it to a integer? im using
    SELECT EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000;
  • s

    Scott P

    03/03/2022, 7:25 PM
    Did you try googling "postgres text to int"?
  • f

    Facu

    03/03/2022, 7:27 PM
    its supose to work with ::integer but still give me a str thats why i ask
  • a

    asleepingpanda

    03/04/2022, 3:27 AM
    I've set up the following function which intends to set the
    modified_by
    to the current user's uuid. All the triggers are set correctly, but when i update a row from the Table Editor, nothing happens. Am I missing something?
    Copy code
    -- Create function for updating the modified_by column
    CREATE OR REPLACE FUNCTION sync_modified_by() 
    RETURNS trigger AS 
    $$
    BEGIN
      NEW.modified_by := auth.uid();
    
      RETURN NEW;
    END;
    $$ LANGUAGE plpgsql;
  • g

    garyaustin

    03/04/2022, 3:35 AM
    Could be a couple of things, but just glancing.... auth.uid() would be null for table editor I would think. 2nd, the table editor, unless a new fix has rolled in already, updates the entire row no matter which column you change. This caused problems with generated columns, but they would error. I would think a trigger could override any data from Table Editor though on that column.
  • a

    asleepingpanda

    03/04/2022, 3:38 AM
    So you’re thinking that once I’ve got my UI hooked up with legit users, it’ll work alright? It’s just returning null cause of the Table Editor being the entry point to the update?
  • g

    garyaustin

    03/04/2022, 3:39 AM
    If you are getting null, that is probably it. auth.uid only works with a jwt token from the api.
  • a

    asleepingpanda

    03/05/2022, 3:04 AM
    Super confused by this. I have the following function. When I run the query as just a query, it returns all of my data just fine. When I try to run it in this function, it throws an error Query:
    Copy code
    CREATE OR REPLACE FUNCTION get_folder_tree(sk varchar)
    RETURNS TABLE (id int, name text, parent_id int, shortkey varchar)
    AS $$
    BEGIN
      RETURN QUERY
        WITH RECURSIVE 
          -- starting node(s)
          starting (id, name, parent_id, shortkey) AS
          (
            SELECT t.id, t.name, t.parent_id, t.shortkey
            FROM folders AS t
            WHERE t.shortkey = sk
          ),
          descendants (id, name, parent_id, shortkey) AS
          (
            SELECT s.id, s.name, s.parent_id, s.shortkey
            FROM starting AS s
            UNION ALL
            SELECT t.id, t.name, t.parent_id, t.shortkey
            FROM folders AS t JOIN descendants AS d ON t.parent_id = d.id
          ),
          ancestors (id, name, parent_id, shortkey) AS
          (
            SELECT t.id, t.name, t.parent_id, t.shortkey
            FROM folders AS t 
            WHERE t.id IN (SELECT parent_id FROM starting)
            UNION ALL
            SELECT t.id, t.name, t.parent_id, t.shortkey
            FROM folders AS t JOIN ancestors AS a ON t.id = a.parent_id
          )
        TABLE ancestors
        UNION ALL
        TABLE descendants ;
    
    END;
    $$ LANGUAGE plpgsql;
    Error:
    column reference "parent_id" is ambiguous
  • g

    garyaustin

    03/05/2022, 3:28 AM
    Don't know if any SQL guru's will come along soon and not sure why function would matter, but I'd look at WHERE t.id IN (SELECT parent_id FROM starting) . As the error is it does not know which parent_id you are referring to. You must be awakingpanda to write a function like that. Good luck.
  • t

    tourdownunder

    03/05/2022, 4:00 AM
    That could be it. Try
    Copy code
    sql
    (SELECT s.parent_id FROM starting s)
    a
    • 2
    • 2
  • a

    asleepingpanda

    03/05/2022, 4:32 AM
    Crazy SQL Function
1...353637...52Latest