ID when use upsert, connectOrCreate and things like that
See original GitHub issueI was really struggling yesterday with an update query to a table with many different relations. I spent hours to understand the errors and find the solution. So yesterday i gave up and i tried it recently and found a solution. But the solution is not that clean as i wanted. Maybe its just my opinion.
Here ist what i wanted to do.
this.prisma.blogs.update({
where: {
id: req.body.id
},
data: {
title: req.body.title,
text: req.body.text,
images: {
connect: { id: req.body.images_id }
},
embed: req.body.embed,
embedType: req.body.embedType,
categories: {
connect: { id: req.body.categories_id }
},
sources: {
connectOrCreate: req.body.sources.map((source: any) => { return {
where: { id: source.id ? source.id : 0 },
create: { name: source.name, url: source.url }
}})
},
blogs_has_tags: {
upsert: req.body.tags.map((tag: any) => { return {
where: {
blogs_id_tags_id: {
blogs_id: req.body.id,
tags_id: tag.id ? tag.id : 0
}
},
create: {
tags: {
connectOrCreate: {
where: { id: tag.id ? tag.id : 0 },
create: { name: tag.name }
}
}
},
update: {
tags: {
connectOrCreate: {
where: { id: tag.id ? tag.id : 0 },
create: { name: tag.name }
}
}
}
}})
},
updatedAt: new Date()
},
include: {
categories: true,
images: true,
blogs_has_tags: {
include: {
tags: true
}
},
users: {
select: {
realName: true
}
},
sources: true
}
})
Take a look at the where definitions of the relations. I pass an array of elements with maybe new elements and i have to check if the new element in the relation table exists or not, when not create a new record for it. In the documentation i read that you are focused to use the where definition, what is no problem at the moment. Because i have to check existence. But what me bothered is that you are focused to pass a value. So for id its an integer. The problem with new elements, that you want to add to the table, is that they dont have an id yet. Yesterday i got errors in my query at the point where i wanted to check the new element if its exists in the table, because the id field was undefined. So in the where definition i have to check if my id field exists, when not then pass a fake id, in my example just 0.
So my request would be, when you work with relations and you use things like connectOrCreate, then it would be very cool, when the id check in the where definition would accept undefined. So its not needed to pass a fake value. I hope you can understand what i mean with this.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:5 (1 by maintainers)
Top GitHub Comments
Related: https://github.com/prisma/prisma/issues/4648
Maybe we should make both operations optional here.
This also creates a bad user experience when using
uuid
s asID
s. Since you can’t passundefined
ornull
but instead have to generate a fakeuuid
.I had to do this:
What I would expect is if I pass
null
/undefined
in thewhere
clause, it would go to thecreate
clause resulting in much cleaner code.