0

I am using Prisma as my schema and migrating it to supabase with prisma migrate dev

One of my tables Profiles, should reference the auth.users table in supabase, in sql something like this id uuid references auth.users not null,

Now since that table is automatically created in supabase do I still add it to my prisma schema? It's not in public either it is in auth.

model Profiles {
  id               String   @id @db.Uuid
  role             String
  subId            String
  stripeCustomerId String
  refundId         String[]
  createdAt        DateTime @default(now())
  updatedAt        DateTime @updatedAt
}

The reason I want the relation is because I want a trigger to automatically run a function that inserts an id and role into the profiles table when a new users is invited.

This is that trigger and function

-- inserts a row into public.profiles
create function public.handle_new_user() 
returns trigger 
language plpgsql 
security definer 
as $$
begin
  insert into public.Profiles (id, role)
  values (new.id, 'BASE_USER');
  return new;
end;
$$;

-- trigger the function every time a user is created
create trigger on_auth_user_created
  after insert on auth.users
  for each row execute procedure public.handle_new_user();

I had this working when I created the profiles table manually in supabase I included the reference to the auth.users, that's the only reason I can think of why the user Id and role won't insert into the profiles db when I invite a user, the trigger and function are failing

create table public.Profiles (
  id uuid references auth.users not null,
  role text,

  primary key (id)
);

Update from comment:
One error I found is

relation "public.profiles" does not exist

I change it to "public.Profiles" with a capital in supabase, but the function seem to still be looking for lowercase.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
Anders Kitson
  • 1,413
  • 6
  • 38
  • 98
  • *Always* disclose the Postgres version in use. `SELECT version();`. And the verbatim error message! And the *exact* table definition, most importantly the *exact* FK definition. *"Something like"* is not good enough. I guess it's about foreign key violation ... – Erwin Brandstetter Dec 21 '21 at 16:14
  • PostGresSQL, I can't seem to find the exact error in the database logs. I will search for a foreign key error. Update: I dont see a foreign key violation error. I am just not sure where to find the error in the logs, I just got a feed back UI error in supabase saying user creation failed, but when I deleted the function and trigger the create user worked fine, and when I created the profiles table with sql command it worked with the trigger and function it's when I created the profiles table with prisma that the trigger and function no longer work – Anders Kitson Dec 21 '21 at 16:19
  • [No such thing as "PostGresSQL".](https://wiki.postgresql.org/wiki/Identity_Guidelines) I am asking for [your *version* of Postgres](https://www.postgresql.org/support/versioning/). And try harder to find the all-important error message. – Erwin Brandstetter Dec 21 '21 at 16:24
  • this is the output of the command in the SQL editor SELECT version(); PostgreSQL 14.1 on aarch64-unknown-linux-gnu, compiled by gcc (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0, 64-bit – Anders Kitson Dec 21 '21 at 16:28
  • One error I found is `relation "public.profiles" does not exist` I change it to "public.Profiles" with a capital in supabase, but the function seem to still be looking for lowercase. I will try and recreate the function and trigger with a capital and see – Anders Kitson Dec 21 '21 at 16:40
  • Did you create your table as `"Profiles"` or as `Profiles`? See: https://stackoverflow.com/a/20880247/939860 – Erwin Brandstetter Dec 21 '21 at 16:40

1 Answers1

3

What you show should just work:

db<>fiddle here

Looks like you messed up capitalization with Postgres identifiers.

If you (or your ORM) created the table as "Profiles" (with double-quotes), non-standard capitalization is preserved and you need to double-quote the name for the rest of its life.

So the trigger function body must read:

...
insert into public."Profiles" (id, role)  -- with double-quotes
...

Note that schema and table (and column) have to be quoted separately.

See:

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228