You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
I'm developing a Supabase app locally. It has a public.profiles table that is referencing the auth.users table to be able to access users data via the API. The definition for the public.profiles table is in a migration file and it has a column which defaults to a custom nanoid() function declared in an earlier migration file. To sync the two tables together I use a trigger on the auth.users table. When I try to seed the DB by creating users I get this error:
Seeding data from supabase/seed.sql...
failed to send batch: ERROR: function nanoid_optimized(integer, text, integer, integer) does not exist (SQLSTATE 42883)
CREATE EXTENSION IF NOT EXISTS pgcrypto;
-- The `nanoid()` function generates a compact, URL-friendly unique identifier.-- Based on the given size and alphabet, it creates a randomized string that's ideal for-- use-cases requiring small, unpredictable IDs (e.g., URL shorteners, generated file names, etc.).-- While it comes with a default configuration, the function is designed to be flexible,-- allowing for customization to meet specific needs.DROPFUNCTION IF EXISTS nanoid(int, text, float);
CREATE OR REPLACEFUNCTIONnanoid(
size int DEFAULT 21, -- The number of symbols in the NanoId String. Must be greater than 0.
alphabet text DEFAULT '_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', -- The symbols used in the NanoId String. Must contain between 1 and 255 symbols.
additionalBytesFactor float DEFAULT 1.6-- The additional bytes factor used for calculating the step size. Must be equal or greater then 1.
)
RETURNS text-- A randomly generated NanoId String
LANGUAGE plpgsql
VOLATILE
PARALLEL SAFE
-- Uncomment the following line if you have superuser privileges-- LEAKPROOFAS
$$
DECLARE
alphabetArray text[];
alphabetLength int :=64;
mask int :=63;
step int :=34;
BEGIN
IF size IS NULLOR size <1 THEN
RAISE EXCEPTION 'The size must be defined and greater than 0!';
END IF;
IF alphabet IS NULLOR length(alphabet) =0OR length(alphabet) >255 THEN
RAISE EXCEPTION 'The alphabet can''t be undefined, zero or bigger than 255 symbols!';
END IF;
IF additionalBytesFactor IS NULLOR additionalBytesFactor <1 THEN
RAISE EXCEPTION 'The additional bytes factor can''t be less than 1!';
END IF;
alphabetArray := regexp_split_to_array(alphabet, '');
alphabetLength := array_length(alphabetArray, 1);
mask := (2<< cast(floor(log(alphabetLength -1) / log(2)) asint)) -1;
step := cast(ceil(additionalBytesFactor * mask * size / alphabetLength) ASint);
IF step >1024 THEN
step :=1024; -- The step size % can''t be bigger then 1024!
END IF;
RETURN nanoid_optimized(size, alphabet, mask, step);
END
$$;
-- Generates an optimized random string of a specified size using the given alphabet, mask, and step.-- This optimized version is designed for higher performance and lower memory overhead.-- No checks are performed! Use it only if you really know what you are doing.DROPFUNCTION IF EXISTS nanoid_optimized(int, text, int, int);
CREATE OR REPLACEFUNCTIONnanoid_optimized(
size int, -- The desired length of the generated string.
alphabet text, -- The set of characters to choose from for generating the string.
mask int, -- The mask used for mapping random bytes to alphabet indices. Should be `(2^n) - 1` where `n` is a power of 2 less than or equal to the alphabet size.
step int-- The number of random bytes to generate in each iteration. A larger value may speed up the function but increase memory usage.
)
RETURNS text-- A randomly generated NanoId String
LANGUAGE plpgsql
VOLATILE
PARALLEL SAFE
-- Uncomment the following line if you have superuser privileges-- LEAKPROOFAS
$$
DECLARE
idBuilder text :='';
counter int :=0;
bytes bytea;
alphabetIndex int;
alphabetArray text[];
alphabetLength int :=64;
BEGIN
alphabetArray := regexp_split_to_array(alphabet, '');
alphabetLength := array_length(alphabetArray, 1);
LOOP
bytes := gen_random_bytes(step);
FOR counter IN0..step -1
LOOP
alphabetIndex := (get_byte(bytes, counter) & mask) +1;
IF alphabetIndex <= alphabetLength THEN
idBuilder := idBuilder || alphabetArray[alphabetIndex];
IF length(idBuilder) = size THEN
RETURN idBuilder;
END IF;
END IF;
END LOOP;
END LOOP;
END
$$;
Create a migration file 2_profiles.sql:
create table
public.profiles (
id uuid not null,
email textnot null,
referral_code textnull default nanoid (8),
constraint profiles_pkey primary key (id),
constraint profiles_referral_code_key unique (referral_code),
constraint profiles_id_fkey foreign key (id) referencesauth.users (id) onupdate cascade on delete cascade
) tablespace pg_default;
create or replacefunctionpublic.handle_new_user()
returns trigger
set search_path =''as $$
begininsert intopublic.profiles (id, email)
values (new.id, new.email);
return new;
end;
$$ language plpgsql security definer;
createtriggeron_auth_user_created
after insert onauth.users
for each row execute procedure public.handle_new_user();
The mistake was on my part and it was due to schema confusion. The trigger is in the auth schema, makes calls to nanoid_optimized() function that is defined in the public schema which makes calls to functions from pgcrypto that are declared in the extensions schema. The solution is to set the search_path in the nanoid functions or add explicit schema names in front of all function calls.
Describe the bug
I'm developing a Supabase app locally. It has a
public.profiles
table that is referencing theauth.users
table to be able to access users data via the API. The definition for thepublic.profiles
table is in a migration file and it has a column which defaults to a customnanoid()
function declared in an earlier migration file. To sync the two tables together I use a trigger on theauth.users
table. When I try to seed the DB by creating users I get this error:Is it a bug or is there something that I don't understand in this pipeline? The
nanoid()
function comes from this repo: https://github.com/viascom/nanoid-postgresTo Reproduce
Steps to reproduce the behavior:
1_functions.sql
:2_profiles.sql
:seed.sql
file:supabase db reset --local
commandI'm currently using the beta branch of the CLI because of this bug #2734
The text was updated successfully, but these errors were encountered: