If you are still down here without an answer, I used a combination from @Antoine's answer and another SO answer:
model Likes {
id String @id @unique @default(uuid())
user_id String
tag String
auth_user AuthUser @relation(references: [id], fields: [user_id], onDelete: Cascade)
@@unique([user_id, tag], name: "user_id_tag") // <-- this is the unique constraint
@@index([user_id])
@@map("likes")
}
Then I was able to upsert via the following:
prisma.likes.upsert({
where: {
user_id_tag: { // <-- And this bit is important
user_id: user.userId,
tag: tag.tag
}
},
update: {},
create: tag
})
Answer from Gavin on Stack Overflow Top answer 1 of 5
25
If you are still down here without an answer, I used a combination from @Antoine's answer and another SO answer:
model Likes {
id String @id @unique @default(uuid())
user_id String
tag String
auth_user AuthUser @relation(references: [id], fields: [user_id], onDelete: Cascade)
@@unique([user_id, tag], name: "user_id_tag") // <-- this is the unique constraint
@@index([user_id])
@@map("likes")
}
Then I was able to upsert via the following:
prisma.likes.upsert({
where: {
user_id_tag: { // <-- And this bit is important
user_id: user.userId,
tag: tag.tag
}
},
update: {},
create: tag
})
2 of 5
24
The fields in where need to be unique.
If you can make some field, let's say date @unique (date: DateTime! @unique), and use that for your where in the upsert, I think it would work (tested on my local)
database - How to use upsert with Prisma? - Stack Overflow
So I’m back to same behaviour as when I used upsert with where, update and create inside it. (See description). So, if I’m getting an empty array back when I’m trying to get the record from the profile table, and I’m trying to pass id which isn’t there, could that be the issue? 2022-09-30T12:25:38.613Z+00:00 ... Somehow, under the hood Prisma ... More on stackoverflow.com
`upsert()` is not possible on MongoDB
Creating an upsert on mongoDB is not possible because an id is always required. Let's consider this case · return this.prisma.entity.upsert({ where: { id: id }, create: createData, update: updateData, }); More on github.com
Prisma `upsert()` introduces ID gaps
Bug description Prisma.upsert seems to reserve the next sequence ID (see the function nextval in Postgres for further information) before checking whether the entry already exists (which means the ... More on github.com
Prisma Mysql backend, update recipe upsert without where
I am attempting to do an update on my database for the following api call on my express.js server. The issue I am running into is that on the front-end I am able to add in new recipe_ingredients, w... More on stackoverflow.com
Daily Dev Tips
daily-dev-tips.com › posts › how-to-perform-non-updating-upserts-in-prisma
Non-updating Upserts in Prisma [2024]
February 5, 2024 - The Prisma upsert command takes a where-query. Where-queries should query a unique field in the DB. Here we upsert without an ID per se, because the URI is good enough as an identifier.
YouTube
youtube.com › hey delphi
NodeJS : How to upsert new record in Prisma without an ID? - YouTube
NodeJS : How to upsert new record in Prisma without an ID?To Access My Live Chat Page, On Google, Search for "hows tech developer connect"As promised, I'm go...
Published April 13, 2023 Views 354
GitHub
github.com › prisma › prisma › discussions › 16882
Using upsert · prisma/prisma · Discussion #16882
Our front-end uses a single page for both add and edit functions. It's only an "edit" when there's an ID, otherwise it's an "add". Problem is, prisma throws an error when id is undefined because obviously that becomes an invalid query.
Author prisma
GitHub
github.com › prisma › prisma › issues › 20157
`upsert()` is not possible on MongoDB · Issue #20157 · prisma/prisma
July 10, 2023 - Creating an upsert on mongoDB is not possible because an id is always required. Let's consider this case · return this.prisma.entity.upsert({ where: { id: id }, create: createData, update: updateData, }); In case we need a creation, there's ...
Author prisma
Blogger
styjun.blogspot.com › 2019 › 09 › how-to-upsert-new-record-in-prisma.html
How to upsert new record in Prisma without an ID?How do I debug Node.js applications?How do I get started with Node.jsHow do I pass command line arguments to a Node.js program?How to decide when to use Node.js?How to exit in Node.jsWhat is the purpose of Node.js module.exports and how do you use it?How can I update NodeJS and NPM to the next versions?How do I update each dependency in package.json to the latest version?npm throws error without sudoPrisma returning error of expected input type but type is pr
September 23, 2019 - The only solution I have found is check for existence and then update or create, but I wanted to do it with upsert. let check = await prisma.$exists.SystemPerformance( system: name: 's1', date: "2019-03-12T00:01:06.000Z" ); let perfo; if (check) const sysPerf = await prisma.systemPerformances(where:system: name: 's1', date: "2019-03-12T00:01:06.000Z") .$fragment(` id `); perfo = await prisma.updateSystemPerformance( where: id: sysPerf[0].id, data: perf1: 13.45, perf2: 18.93 ) else perfo = await prisma.createSystemPerformance( system: connect: name: 's1' , date: "2019-03-12T00:01:06.000Z", perf1: 13.45, perf2: 18.93 )
Prisma
prisma.io › home › prisma client api › prisma client api › prisma client api
Prisma Client API | Prisma Documentation
INSERT INTO "public"."User" ("id","profileViews","userName","email") VALUES ($1,$2,$3,$4) ON CONFLICT ("userName") DO UPDATE SET "email" = $5 WHERE ("public"."User"."userName" = $6 AND 1=1) RETURNING "public"."User"."id", "public"."User"."profileViews", "public"."User"."userName", "public"."User"."email" The following query has multiple unique values in the where clause, so Prisma Client does not use a database upsert:
GitHub
github.com › prisma › prisma › issues › 23197
Prisma `upsert()` introduces ID gaps · Issue #23197 · prisma/prisma
February 20, 2024 - Prisma.upsert seems to reserve the next sequence ID (see the function nextval in Postgres for further information) before checking whether the entry already exists (which means the entry needs to be updated) or not (needs to be created).
Author prisma
GitHub
github.com › prisma › prisma › issues › 5233
`upsert()` `where` is defined as `number | undefined` but does not allow `undefined` to not select anything · Issue #5233 · prisma/prisma
January 21, 2021 - I must specify where: {unique_id: 0} to get it working or am I missing something? Reactions are currently unavailable · No one assigned · tech/typescriptIssue for tech TypeScript.Issue for tech TypeScript.topic: client typesTypes in Prisma ClientTypes in Prisma Clienttopic: undefinedtopic: upsertnested upsertnested upserttopic: upsert() No type · No fields configured for issues without a type.
Author prisma
Answer Overflow
answeroverflow.com › m › 1086787706497028136
.upsert when no way to track query/track unique identifier? - Theo's Typesafe Cult
March 18, 2023 - } I guess my question is 2 fold: 1) Can I use upsert/this design database? Or do I need to rethink my approach. 2) If I can make this work, what's the correct way to resolve this. ... From what I understand, older versions of Prisma only expose unique fields (see the type UserWhereUniqueInput) on your models as fields in the where object when upsert is being used.
Goprisma
goprisma.org › docs › walkthrough › upsert
Upsert records – Prisma Client Go
October 22, 2024 - post, err := client.Post.UpsertOne( // query db.Post.ID.Equals("upsert"), ).Create( // set these fields if document doesn't exist already db.Post.Published.Set(true), db.Post.Title.Set("title"), db.Post.ID.Set("upsert"), ).Update( // update these fields if document already exists db.Post.Title.Set("new-title"), db.Post.Views.Increment(1), ).Exec(ctx)
DEV Community
dev.to › this-is-learning › its-prisma-time-update-1mmi
It's Prisma Time - Update - DEV Community
January 7, 2022 - As you can see, the update method used the patter prisma.[entity].update, not so different from the insert and the delete, obviously the update method updates an existing row. And another thing, if the update method doesn't find the record, it throws an exception that you have to handle in your code. If you execute that code you get this result. { updatedAuthor: { id: 3, firstName: 'Updated first name', lastName: 'Updated last name' } }
GitHub
github.com › prisma › prisma › discussions › 5929
upsert where @@unique([id1, id2]) create missing relationship attributes as connect · prisma/prisma · Discussion #5929
Looks like when you use a upsert with a where that uses a @@unique([]) the create will not allow attaching connections for belongsTo and hasMany..... model Trigger { id String @id @default(cuid()) funnel Funnel @relation(fields: [funnelId], references: [id]) funnelId String product Product @relation(fields: [productId], references: [id]) productId String variants Variant[] @@unique([funnelId, productId]) } return await context.prisma.trigger.upsert({ where: { funnelId_productId: { funnelId: localFunnelId, productId: localProductId, }, }, create: { funnel: { connect: { id: localFunnelId } }, pr
Author prisma
GitHub
github.com › prisma › prisma › issues › 4747
ID when use `upsert`, `connectOrCreate` and things like that · Issue #4747 · prisma/prisma
December 21, 2020 - 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 ?
Author prisma