Help, how do I do this?

Each user has multiple workspaces. However, a user can only be using one workspace at a time (active workspaceId)
4 Replies
gybia
gybia2y ago
(here's the error on the squiggly lines:)
Error validating model "User": Ambiguous relation detected. The fields `workspaces` and `activeWorkspace` in model `User` both refer to `Workspace`. Please provide different relation names for them by adding `@relation(<name>).
Error validating model "User": Ambiguous relation detected. The fields `workspaces` and `activeWorkspace` in model `User` both refer to `Workspace`. Please provide different relation names for them by adding `@relation(<name>).
cje
cje2y ago
the error contains the answer
cje
cje2y ago
Prisma
Relations (Reference)
A relation is a connection between two models in the Prisma schema. This page explains how you can define one-to-one, one-to-many and many-to-many relations in Prisma.
gybia
gybia2y ago
Thank you. This is what I did: I think I solved it
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
workspaces Workspace[] @relation("UserWorkspace")
activeWorkspaceId String? @map("activeWorkspaceId")
activeWorkspace Workspace? @relation("UserActiveWorkspace", fields: [activeWorkspaceId], references: [id])
}

model Workspace {
id String @id @default(cuid())
name String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
users User[] @relation("UserWorkspace")
activeUsers User[] @relation("UserActiveWorkspace")
activeApps App[]
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
workspaces Workspace[] @relation("UserWorkspace")
activeWorkspaceId String? @map("activeWorkspaceId")
activeWorkspace Workspace? @relation("UserActiveWorkspace", fields: [activeWorkspaceId], references: [id])
}

model Workspace {
id String @id @default(cuid())
name String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
users User[] @relation("UserWorkspace")
activeUsers User[] @relation("UserActiveWorkspace")
activeApps App[]
}
Want results from more Discord servers?
Add your server