prisma: Cross schema reference with foreign key not working in 4.7.0+
Bug description
Running prisma migrate dev
gives me following error:
Error: P4002
The schema of the introspected database was inconsistent: Illegal cross schema reference from `public.Users` to `auth.users` in constraint `Users_authId_fkey`. Foreign keys between database schemas are not supported in Prisma. Please follow the GitHub ticket: https://github.com/prisma/prisma/issues/1175
I am using postgres + supabase auth and cross reference is used from public schema to auth schema.
How to reproduce
- Use two separate postgres schemas
- Reference with foreign key from one table to another
- Run prisma migrate dev
- See error
Error: P4002
The schema of the introspected database was inconsistent: Illegal cross schema reference from `public.Users` to `auth.users` in constraint `Users_authId_fkey`. Foreign keys between database schemas are not supported in Prisma. Please follow the GitHub ticket: https://github.com/prisma/prisma/issues/1175
Expected behavior
No response
Prisma information
Prisma & prisma-client 4.7.0
Environment & setup
- OS: MacOS
- Database: Postgres 14.1 (supabase)
- Node.js version:
Prisma Version
generator client {
provider = "prisma-client-js"
previewFeatures = ["multiSchema"]
binaryTargets = ["native", "rhel-openssl-1.0.x"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
schemas = ["auth", "public"]
}
model User {
id Int @id @default(autoincrement())
authId String @unique @db.Uuid
createdAt DateTime @default(now())
users users @relation(fields: [authId], references: [id], onDelete: Cascade, onUpdate: NoAction)
@@map("Users")
@@schema("public")
}
model users {
instance_id String? @db.Uuid
id String @id @db.Uuid
aud String? @db.VarChar(255)
role String? @db.VarChar(255)
email String? @unique @db.VarChar(255)
encrypted_password String? @db.VarChar(255)
email_confirmed_at DateTime? @db.Timestamptz(6)
invited_at DateTime? @db.Timestamptz(6)
confirmation_token String? @db.VarChar(255)
confirmation_sent_at DateTime? @db.Timestamptz(6)
recovery_token String? @db.VarChar(255)
recovery_sent_at DateTime? @db.Timestamptz(6)
email_change_token_new String? @db.VarChar(255)
email_change String? @db.VarChar(255)
email_change_sent_at DateTime? @db.Timestamptz(6)
last_sign_in_at DateTime? @db.Timestamptz(6)
raw_app_meta_data Json?
raw_user_meta_data Json?
is_super_admin Boolean?
created_at DateTime? @db.Timestamptz(6)
updated_at DateTime? @db.Timestamptz(6)
phone String? @unique @db.VarChar(15)
phone_confirmed_at DateTime? @db.Timestamptz(6)
phone_change String? @default("") @db.VarChar(15)
phone_change_token String? @default("") @db.VarChar(255)
phone_change_sent_at DateTime? @db.Timestamptz(6)
confirmed_at DateTime? @default(dbgenerated("LEAST(email_confirmed_at, phone_confirmed_at)")) @db.Timestamptz(6)
email_change_token_current String? @default("") @db.VarChar(255)
email_change_confirm_status Int? @default(0) @db.SmallInt
banned_until DateTime? @db.Timestamptz(6)
reauthentication_token String? @default("") @db.VarChar(255)
reauthentication_sent_at DateTime? @db.Timestamptz(6)
identities identities[]
mfa_factors mfa_factors[]
sessions sessions[]
User User?
@@index([instance_id])
@@schema("auth")
}
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 5
- Comments: 22 (13 by maintainers)
Commits related to this issue
- me: pass namespaces to diff in devDiagnostic->diagnoseMigrationHistory The namespaces are from the schema the engine was started with This should address https://github.com/prisma/prisma/issues/1663... — committed to prisma/prisma-engines by tomhoule 2 years ago
- me: pass namespaces to diff in devDiagnostic->diagnoseMigrationHistory The namespaces are from the schema the engine was started with This should address https://github.com/prisma/prisma/issues/1663... — committed to prisma/prisma-engines by tomhoule 2 years ago
- me: pass namespaces to diff in devDiagnostic->diagnoseMigrationHistory (#3481) The namespaces are from the schema the engine was started with This should address https://github.com/prisma/prisma/i... — committed to prisma/prisma-engines by tomhoule 2 years ago
Note: it’s going in tomorrow’s, 4.8.0 release
I’m having this same problem after doing a db pull from the default database for this Next.JS + Supabase starter project.
After doing some name mapping to get rid of the
model user
vsmodel user
clash, I’m getting this complaint when I runprisma migrate dev
:It seems clear to me that my
customers
foreign key is pointing to thepublic
schema’suser
model and not theauth
schema’sAuthUser
(mapped touser
) model, so this seems like a bug.I’m very new to Prisma so definitely possible I’m just screwing something up, but I figured this might be helpful for you to know since it should be easily reproducible using that starter project.
Here is my
schema.prisma
file (Profile
is the only custom table I’ve created that wasn’t already in the starter project db.)Hi! Sorry for late reply but in my case I used both approaches. I first wrote the “User” - model into the schema and ran
prisma migrate
. After this I added the “auth” schema into the prisma schema file, so that it recognizes the supabase auth tables. Then I executedprisma db pull
command which imported the supabase auth tables into my prisma schema file. Finally I added the relation from public.User table to auth.users table. This is when the error occurred as I tried to migrate the changes.I can also verify both @firzanarmani and @mjlatty cases, as they are basically same setups as I have.
I’d like to offer another reproduction of this bug on Supabase. (Hope it somehow helps)
How to reproduce
SQL snippet here for reference
prisma db pull
to introspectNew schema.prisma looks like this
prisma migrate dev --name init
Prisma Version
Yes, I think that matches what Julius found out later as well. We’ll have a fix for this hopefully soon to test.
Sorry @janpio I got mixed up between dev and deploy 🤦♂️ re-running now with
yarn prisma migrate deploy
returns the error P4002, I can send over my init migration file if that helpers too?@janpio here’s the full output of
migrate dev
for me:Before running
prisma db pull
, my schema looks like thisWorth noting that, similar to your reproduction attempt:
I find these to be true as well, which led me to using the schema as above.
Using
4.8.0-dev.22
internal version or 4.7.1 (PostgreSQL 10.21) Note I had to create manually the auth schema, the provided SQL was not enough.With the following schema
npx prisma db pull
also errorsWith
npx prisma db pull
does not error, onlymigrate dev