How to use Prisma with Supabase Auth?

Hi, does anyone know how to set up Prisma with Supabase Auth? I'm using Supabase Auth and when I try to set up Prisma, it gives the error "The schema of the introspected database was inconsistent: Illegal cross schema reference from public.profiles to auth.users" 😦 Would anyone please help me?
60 Replies
Olyno
Olyno•4y ago
Hi :vmathi: Please have a look here: https://github.com/supabase/supabase/issues/1502 @trilltrill
GitHub
Issues cross-referencing tables from different schemas using Prisma...
Feature request Is your feature request related to a problem? Please describe. I am trying to use Supabase together with Prisma. There seems to be a problem cross-referencing tables in different sc...
NanoBit
NanoBit•4y ago
For my case, I do not put the trigger from auth.users -> public.profiles inside Prisma migrations . I apply it externally via psql. I created a Discussion for Prisma+Supabase issues here: https://github.com/supabase/supabase/discussions/7659 Your Issue is #1,2
GitHub
Supabase and Prisma workflow compilation Ā· Discussion #7659 Ā· supab...
Note: This is a updated copy from the discord's help channel to increase visibility. I hope this can also be a central source for pointing to Prisma issues and workaround. Cross-posted to p...
trilltrill
trilltrillOP•4y ago
@NanoBit what do you mean by "apply it externally via psql"?
NanoBit
NanoBit•4y ago
Ah, so I have a package.json file which I added a script: say: prisma:reset: "yarn prisma migrate reset && psql -f /path/to/sql/file $DATABASE_URL" so, it always applies the psql after applying prisma migrations. Of course, it's not the best solution and I'm open to ideas..
trilltrill
trilltrillOP•4y ago
:/ gotchuu what im doing right now is just setting up a trigger for when auth.user is created to create a public.profile setting up a trigger for when auth.user is deleted to delete the row in public.profile and then just using public.profile to add data but public.profile's id isnt linked to the auth.user it's just a plain text of the id im not sure if that's how im supposed to do it though, or what limitations ill run into later 😢
NanoBit
NanoBit•4y ago
yes, that's similar to what I have but, how do you apply the trigger
trilltrill
trilltrillOP•4y ago
i have a trigger set up linked to a function on supabase
NanoBit
NanoBit•4y ago
ah, you mean on the supabase hosted db right?
trilltrill
trilltrillOP•4y ago
yeah its on the actual supabase.com
NanoBit
NanoBit•4y ago
yea, you can run it once, and it'll be good
trilltrill
trilltrillOP•4y ago
yeah
NanoBit
NanoBit•4y ago
for me, this is a major problem on dev because prisma migrations always reset
trilltrill
trilltrillOP•4y ago
i dont really understand haha so would my solution not work for dev?
NanoBit
NanoBit•4y ago
It would definitely It's just that, it may get unapplied when you call prisma migrate reset
trilltrill
trilltrillOP•4y ago
what does that command do again? when would you iuse it?
NanoBit
NanoBit•4y ago
it resets the development db and adds all the migrations back
trilltrill
trilltrillOP•4y ago
gotchuu ive never used that command before since i just started out with prisma
NanoBit
NanoBit•4y ago
i think it's just a quality of life to not have to manually go to the DB and paste all the triggers every time we start supabase local development
trilltrill
trilltrillOP•4y ago
yeah do you understand postgres? can you help me look over a snippet of code? to make sure that it does what i intend it to do?
NanoBit
NanoBit•4y ago
sure
trilltrill
trilltrillOP•4y ago
begin delete from public.profiles where id = old.id; return old; end; this is the function i attached to a trigger for when the auth.user is deleted this function will only delete the row from public.profiles that has the id as the auth.user deleted right? is it correct to return "old"?
NanoBit
NanoBit•4y ago
You can see the previous Discussion linked if you meet other Prisma + Supabase issues. It's well worth it (imo), but need to accept drawbacks.
trilltrill
trilltrillOP•4y ago
im scared it will somehow delete more than one profile row haha yeah ill check it out šŸ™‚
NanoBit
NanoBit•4y ago
Hm, so you want that if you delete a user from auth.users to also delete in profile.users right
trilltrill
trilltrillOP•4y ago
no i want to delete a user from auth.users and also delete a profile for that user in public.profiles but only just one profile
NanoBit
NanoBit•4y ago
ah gotcha
trilltrill
trilltrillOP•4y ago
for that one user deleted
NanoBit
NanoBit•4y ago
I would prefer being explicit in the calls in case in confuses which table id belongs to.
delete from public.profiles
where profiles.id = old.id;
return old;
delete from public.profiles
where profiles.id = old.id;
return old;
trilltrill
trilltrillOP•4y ago
ahh ok
NanoBit
NanoBit•4y ago
this should be correct
trilltrill
trilltrillOP•4y ago
šŸ™‚ thanks NanoBit
NanoBit
NanoBit•4y ago
Yes, from what I read. It should be good
trilltrill
trilltrillOP•4y ago
yay thanks thanks!!
NanoBit
NanoBit•4y ago
If you are worried, you can test it. Create two users and delete one
trilltrill
trilltrillOP•4y ago
yep ill do that right now
NanoBit
NanoBit•4y ago
cool!
trilltrill
trilltrillOP•4y ago
thanks a ton
Gruce
Gruce•4y ago
@NanoBit Hello, When I make migrate and reset database my functions and triggers get deleted. How could this be fixed?
NanoBit
NanoBit•4y ago
Which functions/triggers?
Gruce
Gruce•4y ago
Database functions that I have created
NanoBit
NanoBit•4y ago
specifically, functions that are only in public schema, or cross schema (like auth)?
Gruce
Gruce•4y ago
public schema
NanoBit
NanoBit•4y ago
You could put them inside the prisma migrations It will be restored when you prisma migrate
Gruce
Gruce•4y ago
I have only schema pgsodium
NanoBit
NanoBit•4y ago
Uhm, I'm not sure what that is Perhaps, we can move to a different thread as your problem seems different from the OP?
Gruce
Gruce•4y ago
You mean in seed.ts ? Yes.
santi
santi•4y ago
You could put the SQL into one of the earliest migration.sql scripts for Prisma
NanoBit
NanoBit•4y ago
Yep, for stuff within public schema. This is the way to go. However, this won't work for cross-schema triggers/functions. That's why I suggested a separate file applied externally via psql. Unless you have an alternative? @santi
santi
santi•4y ago
Figured something out for the thing I needed @NanoBit We use prisma. Learned that prisma.$queryRawUnsafe('SELECT * FROM auth."users"') works fine In my case, I wanted to get the id of a user by phone So prisma.$queryRawUnsafe('SELECT * FROM auth."users" WHERE phone = \'something\'')
NanoBit
NanoBit•4y ago
Really? That's interesting to say. I have not tested it I'm curious though. @santi Are you using prisma with postgres role or a separate role? I would not imagine that other roles have access to auth schema
santi
santi•4y ago
postgres role
NanoBit
NanoBit•4y ago
Oh! I see. I'll have to note that down. Thanks! I'm using RLS with prisma, so I don't have that option
santi
santi•4y ago
Hmm interesting Do you query prisma directly from the client-side? For us it goes through an API
NanoBit
NanoBit•4y ago
no, it goes through API, but I mimic postgrest's method of applying RLS
santi
santi•4y ago
Hmm interesting In my head I thought the main use-case for RLS was to allow users to query the Prisma themselves, but with limited scope
NanoBit
NanoBit•4y ago
Ah yes. I just used an API in the middle to prevent users from direct access to db (so I can edit data etc before it's saved to db) but also so that, only users with correct permission is allowed to access/write data to db I don't do that check in the API layer do u check user permission in API layer?
santi
santi•4y ago
Yes
NanoBit
NanoBit•4y ago
can i ask what u use? is it just a few global permissions (like teacher vs student) or very finetuned permissions (can access resource A, but not B)?
santi
santi•4y ago
It's pretty simple Basically there's a few routes where the table being edited is user data, so I'll need a userId So I make sure the auth is valid and use that userId when I do the query or mutation
NanoBit
NanoBit•4y ago
Oh, I get you somewhat Good to know! I was actually curious how others implement access control with Prisma

Did you find this page helpful?