-
Good morning everyone. I recently decided to switch from Prisma 1 to Prisma 2 and after changing the schema.prisma file, i tried to get the same behavior as before. However I can't figure out where I did things wrong. When I try to create a message with the mutation createMessage I get the error: Error: Message creation failed
at createMessage (/.../server/graphQL/resolvers/Message.js:29:17)
at processTicksAndRejections (internal/process/task_queues.js:97:5) The hint is that it comes from: const message = await context.prisma.message.create({ data: { text, sentBy: { connect: { id: userId } }, channel: { connect: { id: channelId } } }}) but I don't know if its during prisma schema definition or during resolver I get things wrong. // /typeDefs/Messages.js
type Message {
id: ID!
text: String!
sentBy: User!
channel: Channel!
createdAt: String!
}
type Mutation {
createMessage(channelId: String, recipient: String, text: String!): Boolean!
} // /prisma/schema.prisma
model Message {
id String @id @default(cuid())
text String
sentById String? @map("sentBy")
sentBy User? @relation(fields: [sentById], references: [id])
channelId String? @map("channel")
channel Channel? @relation(fields: [channelId], references: [id])
createdAt DateTime @default(now())
}
model Channel {
id String @id @default(cuid())
users User[] @relation(references: [id])
messages Message[] @relation(references: [id])
createdAt DateTime @default(now())
}
model User {
id String @id @default(cuid())
nom String
avatar String?
prenom String
numero String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
channels Channel[]
offerings Offering[]
messages Message[]
} Environment setup:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Good morning everyone. |
Beta Was this translation helpful? Give feedback.
-
Hey @Rizialdi 👋 I have updated the schema here: model Message {
id String @default(cuid()) @id
text String
sentBy User? @relation(fields: [sentById], references: [id])
sentById String? @map("sentBy")
channel Channel? @relation(fields: [channelId], references: [id])
channelId String? @map("channel")
createdAt DateTime @default(now())
}
model Channel {
id String @default(cuid()) @id
users User[] @relation(references: [id])
messages Message[]
createdAt DateTime @default(now())
}
model User {
id String @default(cuid()) @id
nom String
avatar String?
prenom String
numero String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
channels Channel[]
messages Message[]
} Let me know if this works! You can read more about the new relation syntax here |
Beta Was this translation helpful? Give feedback.
Hey @Rizialdi 👋
You had an issue in your schema, the
@relation
directive is not required on the many side of the relation while using 1-M relationsI have updated the schema here: