This might help: https://www.prisma.io/docs/concepts/components/prisma-schema/relations/referential-actions
I was handling authorization like this that i make a model for users and model for admin and each one has an email and password but now i am trying to refactor my code by merging users and admin into 1 table and handle auths like below
model all_users {
id String @id @default(uuid())
name String
email String
@unique(map: "user_email_key")
password String
role String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user user? admin
admin?
}
model user{
id String?
alluserId String? @unique
user all_users ? @relation(fields: [userId], references: [id])
votes votes[]
}
model admin{
id String?
alluserId String? @unique user
all_users ? @relation(fields: [userId], references: [id])
votes votes[]
}
model votes{
id String @id @default(uuid())
votes Json?
user user @relation(fields: [userId], references: [alluserId])
userId String
admin admin @relation(fields: [adminId], references: [alluserId])
adminId String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt @@index([entrepreneurId], name: "index_votes") }when I use npx prisma db push on a new database it works great but now when I try to make npx prisma db push on my old database so I can use votes i always get this error
insert or update on table "votes" violates foreign key constraint "votes_userId_fkey" 0: sql_schema_connector::apply_migration::migration_step
Error: Database error: Foreign key constraint failed
Bug (D1): PrismaClientKnownRequestError: Foreign key constraint failed on the field: `foreign key`
ERROR: Foreign key constraint failed on the field
Prisma incorrectly parses CRDB's FK constraint error as `not available`
When you try to delete a profile, there might be projects with a foreign key that references a profile. Because of this relation, you will get an error when trying to delete a profile. You can use referential actions. In your case, replace "OnDelete: NoAction with OnDelete: Cascade" inside the relation field in the Project model. (EDIT: Make sure to update your database as well with the new constraint)
You can read more about it here: Referential actions | Prisma docs
You use "profile" and "project", instead use "Profile" and "Project" with first letter uppercase, or use @@map for change the name of your model. (The models are crated with same name from model or the name you set in @@map)
https://www.prisma.io/docs/concepts/components/prisma-schema/data-model#introspection-and-migration