is there a plan to add an upsert to nested writes? The use case is when you dont know if the nested record exists or not
say im creating a movie with a list of n actors, some already exist and some do not.
using either create or connect wont work since i dont know beforehand which exist or not.
the only current solution is to loop and upsert the entire list and then always use connect.
so instead of connect, create also add an upsert
(pseudo ish)
const actors = [{name:'Johnny Dopp'}, {name:'Brad Pitts'}]
const actorIds = Promise.all(actors.map((actor)=>photon.artists.upsert({
where: { name: actor.name },
update: {},
create: actor
}))
photon.movie.create({
name: "something",
cast: {
connect: actorIds.map(actor=>actor.id)
}
})
but instead
photon.movie.create({
name: "something",
cast: {
upsert: [
{name:'Johnny Dopp'},
{name:'Brad Pitts'}
]
}
})
the first one is not very ACID obviously.
You can open an issue about this in the specs repo so that we can include it in the upcoming photonjs API changes: https://github.com/prisma/specs
Also, checkout photonjs spec: https://github.com/prisma/specs/tree/master/photonjs
Internal Note: We do have nested upsert but we only expose it within an update. Exposing this might be very simple. It is mostly writing a few test cases i assume.
Any plans to implement this? @mavilein
it would be an amazing feature
Just realized that we already have a more prominent issue for this feature request: https://github.com/prisma/prisma1/issues/2194
That one is for Prisma1 though, which we do not look into around here ;)
so a feature thats been requested since 2018 and might be very simple. sounds like a good candidate @mavilein :)
@jaywalklabs @mavilein @janpio Agree, this would remove a lot of boilerplate!
@ssalbdivad not no mention actually support transactions which the boilerplate workaround does not.
this would be awesome when it works! 馃
The connectOrCreate nested operation is available on dev and will land on stable with the next release (2.1.0).
Running into an issue with the connectOrCreate operation. The following query should create an entity for a Winery, and assign a Tag to it.
mutation createwinery {
createOneWinery(
data: {
name: "Winery X",
author: { connect: { email: "[email protected]" } },
tag: {
create: {
tagName: {
connectOrCreate: {
where: { name: "Tag 1" }
create: {
name: "Tag 1",
author: { connect: { email: "[email protected]" } }
}
}
}
author: { connect: { email: "[email protected]" } }
}
}
}
) {
name
author {
name
}
tag {
tagName {
name
}
}
}
}
I get the error message in the image below
.

When I replace the connectOrCreate operation with a regular create the query runs as expected, without error.
Moved into separate issue https://github.com/prisma/prisma-client-js/issues/764 @RoaldSchuring
Most helpful comment
The
connectOrCreatenested operation is available ondevand will land on stable with the next release (2.1.0).