Halvor
07/08/2021, 11:14 PMconst match = await prisma.match.findUnique({
where: {
session_id: 1234
}
});
const arbitrated = await prisma.arbitrated.create({
data: {
match: {
connect: {
id: match.id
}
},
arbitrated_session_id: 5678
}
});
Ryan
07/09/2021, 5:14 AMawait prisma.match.update({
where: { session_id: 1234 },
data: {
arbitrated: { create: { arbitrated_session_id: 5678 } },
},
})
Ryan
07/09/2021, 5:31 AMmodel match {
id Int @id @default(autoincrement())
session_id BigInt @unique
arbitrated arbitrated?
}
model arbitrated {
id Int @id @default(autoincrement())
match match? @relation(fields: [matchId], references: [id])
matchId Int?
arbitrated_session_id BigInt @unique
}
The reason being tat as this is a 1-1 relation, you are connecting a new arbitrated
to match
. But the old arbitrated
that was connected will not have a match
anymore. This would cause a constraint error as the relation is required.
Which is why you need to keep the relation optional.Halvor
07/09/2021, 10:45 AMHalvor
07/09/2021, 10:45 AMHalvor
07/09/2021, 10:47 AMRyan
07/09/2021, 10:47 AMarbitrated
to a match
because the old arbitrated
which was already connected will not have a match anymore.Ryan
07/09/2021, 10:48 AMHalvor
07/09/2021, 10:51 AMHalvor
07/09/2021, 10:53 AMRyan
07/09/2021, 11:01 AMeven if the 1-to-1 relation is optional?No then it will work perfectly as I’d sent the schema above.
Ryan
07/09/2021, 11:03 AMJust to be clear, so if updating a match that already has an arbitrated relation the update will fail just return the existing entry?If the relation is required, then yes it will fail.
Halvor
07/09/2021, 11:06 AMmatchId
)" when trying to use that update() query.Halvor
07/09/2021, 11:06 AMRyan
07/09/2021, 11:08 AMthx, now i’m getting “Null constraint violation on the fields: (On optional relations?)” when trying to use that update() query.matchId
Halvor
07/09/2021, 11:09 AMHalvor
07/09/2021, 11:09 AMRyan
07/09/2021, 11:10 AMRyan
07/09/2021, 11:11 AMand if it fails, how would i get the read the data, with another findUnique()?It will never fail for optional relations 🙂
Halvor
07/09/2021, 11:13 AMsession_id
)Halvor
07/09/2021, 11:14 AMRyan
07/09/2021, 11:18 AMHalvor
07/09/2021, 11:22 AMconst match = await prisma.match.update({
where: {
session_id: matchId
},
data: {
arbitrated: {
create: {
session_id: Math.floor(Math.random() * 100)
}
}
},
include: {
arbitrated: true
}
});
Halvor
07/09/2021, 11:23 AMHalvor
07/09/2021, 11:28 AMRyan
07/09/2021, 11:31 AMcreate
will always set the matchId
to the one that you’re updating with. I could send you an example.Halvor
07/09/2021, 11:36 AMHalvor
07/09/2021, 11:37 AMHalvor
07/09/2021, 11:37 AMHalvor
07/09/2021, 11:37 AMRyan
07/09/2021, 11:40 AMarbitrated
to be associated with a single match
, then you need a 1-many relation.Halvor
07/09/2021, 11:41 AMHalvor
07/09/2021, 11:41 AMHalvor
07/09/2021, 11:42 AMconst match = await prisma.match.update({
where: {
session_id: matchId
},
data: {
arbitrated: {
upsert: {
create: {
session_id: Math.floor(Math.random() * 100)
},
update: {}
}
}
},
include: {
arbitrated: true
}
});
Halvor
07/09/2021, 11:43 AMHalvor
07/09/2021, 11:52 AMHalvor
07/09/2021, 11:52 AM