Skip to content

ZModel validation error when @@unique references a field is inherited from a skip-level parent #983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ymc9 opened this issue Feb 3, 2024 · 0 comments
Milestone

Comments

@ymc9
Copy link
Member

ymc9 commented Feb 3, 2024

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = "file:./dev.db"
}

model User {
  id String @id
  orgMember OrgMember[]
}

model Organization {
  id String @id
  members OrgMember[]
  roles OrgMemberRole[]
}

model OrgMember {
  user User @relation(fields: [userId], references: [id])
  userId String
  org Organization @relation(fields: [orgId], references: [id])
  orgId String
  roles OrgMemberRole[]
}

abstract model _BaseEntity {
  id String @id @db.Uuid @default(uuid()) @deny("update", true)
  createdAt DateTime @default(now()) @deny("update", true)
  updatedAt DateTime @updatedAt @deny("update", true)
  deletedAt Int @default(0) @omit @deny("update", deletedAt != 0)

  // deny DELETE in favor of soft delete
  @@deny("delete", true)
  // deny ALL on deleted entities
  @@deny("all", deletedAt != 0)
  // deny ALL if user is not authenticated
  @@deny("all", auth() == null)
}

abstract model _OrganizationEntity extends _BaseEntity {
  org Organization @relation(fields: [orgId], references: [id], onDelete: Cascade)
  orgId String @db.Uuid @deny("update", true)

  // deny ALL if user is not an organization member
  @@deny("all", org.members^[user == auth()])
}

model OrgMemberRole extends _OrganizationEntity {
  name String
  members OrgMember[]

  @@unique([orgId, name, deletedAt]) // DOES NOT WORK. NEEDED SO THAT NEW ROLES CAN HAVE THE SAME NAME AS ALREADY DELETED ONES BUT BE UNIQUE IF NOT DELETED
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant