Hey guys! Who has experience working with Prisma a...
# help
l
Hey guys! Who has experience working with Prisma and Supabase Auth? I want all my models to be defined in my Prisma schema, but I want my authentication to be handled using Supabase. I know how to set up Supabase Auth, and I know how to connect Prisma to Supabase database, but I don't know how to make them work together. As far as I understand, I should create my own Profiles table (managed by Prisma), and use Supabase trigger function to automatically create a record every time the user signs up and is added to the Users table (managed automatically by Supabase). But I don't understand how would I be able to create a relationship between these two tables, how would I create a foreign key in my Prisma schema pointing to the Users table, if the Users table is not defined in my schema, but created automatically by Supabase? Can you help me out?
n
Hello @lumen! This thread has been automatically created from your message in #843999948717555735 a few seconds ago. We have already mentioned the @User so that they can see your message and help you as soon as possible! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ``...`` menu) and select "Leave Thread" to unsubscribe from future updates. Want to change the title? Use the ``/title`` command! We have solved your problem? Click the button below to archive it.
g
I've never used Prisma, but not sure why you need a foreign key back to auth.users. Basically you can have a trigger function for insert/update/delete on auth.users to manage your profile rows. That assumes that trigger part works as you suggest.
n
lumen (2022-05-21)
l
But shouldn't the
profile
rows have a foreign key to the
auth.users
table? I mean I'm not 100% sure, but isn't that how you should always connect related tables? I'm setting things up by following a course, here's the video that says that
profile
table should have a foreign key to `auth.users`: https://egghead.io/lessons/supabase-add-relationships-between-tables-in-supabase-using-foreign-keys (start watching at 48th second) The video shows how to set up the foreign key using UI, but I can't do that in Prisma, since all the Prisma tables and foreign keys are defined in
prisma.schema
file. If I was creating everything in Prisma, I'd do something like:
Copy code
model Profile {
  id     String @id @default(cuid())
  // This is how I would make a foreign key in prisma,
  // if the User table was defined in my schema.prisma file
  user   User   @relation("UserProfile", fields: [userId], references: [id])
  userId String

  // The rest of the fields...
  // [...]
}
But my User table is not defined in my
prisma.schema
file, it's created by Supabase. So how do I create a foreign key from Profile to
user
? If you don't know the answer to this, since you're not a Prisma user, is there any chance you could help me find someone who does use both? I keep hearing that many people are using Prisma and Supabase, but I can't seem to find any code examples or anyone who can help me out with this.
g
If you use the trigger functions to reflect changes in auth.users in your profile table AND put all your needed info in the profile table on those triggers why would you need a foreign key? You just need an uuid in the profile table for the triggers to insert or use for update or delete. If you need some private info from auth.users and your profiles table is public, then you might need another table with different RLS, still in the public schema (like public.users)
If Prisma has it's own user management that you have to use, or integrate into supabase auth, that is different.