Prisma Schema

Hello doing feedback app, i am having problems to update my feedback info, i want to update name and questions. On comments i show schema and current route.
2 Replies
rocawear
rocawearOP2y ago
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
organisations OrganisationsOnUsers[]
feedbacks Feedback[]
answers Answer[]
}

model Organisation {
id String @id @default(cuid())
name String
users OrganisationsOnUsers[]
feedbacks Feedback[]
answers Answer[]
}

model OrganisationsOnUsers {
user User @relation(fields: [userId], references: [id])
userId String
organisation Organisation @relation(fields: [organisationId], references: [id])
organisationId String
role String @default("user")

@@id([userId, organisationId])
}

model Feedback {
id String @id @default(cuid())
name String
organisation Organisation @relation(fields: [organisationId], references: [id])
organisationId String
questions Question[]
answers Answer[]
user User? @relation(fields: [userId], references: [id])
userId String?
}

model Question {
id String @id @default(cuid())
content String
feedback Feedback @relation(fields: [feedbackId], references: [id])
feedbackId String
answers Answer[]
}

model Answer {
id String @id @default(cuid())
value Int
question Question @relation(fields: [questionId], references: [id])
questionId String
user User @relation(fields: [userId], references: [id])
userId String
organisation Organisation @relation(fields: [organisationId], references: [id])
organisationId String
feedback Feedback? @relation(fields: [feedbackId], references: [id])
feedbackId String?

@@unique([userId, questionId])
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
organisations OrganisationsOnUsers[]
feedbacks Feedback[]
answers Answer[]
}

model Organisation {
id String @id @default(cuid())
name String
users OrganisationsOnUsers[]
feedbacks Feedback[]
answers Answer[]
}

model OrganisationsOnUsers {
user User @relation(fields: [userId], references: [id])
userId String
organisation Organisation @relation(fields: [organisationId], references: [id])
organisationId String
role String @default("user")

@@id([userId, organisationId])
}

model Feedback {
id String @id @default(cuid())
name String
organisation Organisation @relation(fields: [organisationId], references: [id])
organisationId String
questions Question[]
answers Answer[]
user User? @relation(fields: [userId], references: [id])
userId String?
}

model Question {
id String @id @default(cuid())
content String
feedback Feedback @relation(fields: [feedbackId], references: [id])
feedbackId String
answers Answer[]
}

model Answer {
id String @id @default(cuid())
value Int
question Question @relation(fields: [questionId], references: [id])
questionId String
user User @relation(fields: [userId], references: [id])
userId String
organisation Organisation @relation(fields: [organisationId], references: [id])
organisationId String
feedback Feedback? @relation(fields: [feedbackId], references: [id])
feedbackId String?

@@unique([userId, questionId])
}
Here not sure how to update it correctly, I using useArrayFields() on my frontend and I am sending data like:
{
"name": "feed",
"questions": [
{
"content": "jkjjj"
},
{
"content": "jkjjjdddd"
}
]
}
{
"name": "feed",
"questions": [
{
"content": "jkjjj"
},
{
"content": "jkjjjdddd"
}
]
}
const j = await db.feedback.update({
where: { id: params.feedbackId },
data: {
name: payload.name,
questions: {},
},
include: { questions: true },
});
const j = await db.feedback.update({
where: { id: params.feedbackId },
data: {
name: payload.name,
questions: {},
},
include: { questions: true },
});
That code updates feedback name as expected but not sure how to update questions so what i would want is to update the feedback to be what i send This sounds abit hacky because i would like to keep history of questions (i am storing answers so those would be gone after update):
const j = await db.feedback.update({
where: { id: params.feedbackId },
data: {
name: payload.name,
questions: {
deleteMany: {},
create: payload.questions,
},
},
include: { questions: true },
});
const j = await db.feedback.update({
where: { id: params.feedbackId },
data: {
name: payload.name,
questions: {
deleteMany: {},
create: payload.questions,
},
},
include: { questions: true },
});
Sybatron
Sybatron2y ago
if you want to keep them shouldn't you update the model and change the reference action and if you want to just update the questions' content can't you send in the payload in each question their id so you can update these ids
Want results from more Discord servers?
Add your server