-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prisma Client upsert returns count instead of data #10935
Comments
@janpio Yes, I referenced this documentation. What do I need to do on my end to fix this issue? |
I am unable to reproduce this. Can you please make sure that you are using |
@pantharshit00 My exact code is in the issue description, I'm not using |
From a conversation I had with @ruheni, here are the logs from the query in this issues description. I got these logs from
|
I've discovered that if you use upsert (when creating a record) it behaves as expected. This was first noted under Prisma 3.6.0 and still persists under 3.8.0.
console.log(dbUser); Returns
on an update But it returns:
When creating data for the first time |
I am still unable to reproduce this. I tried last 3 versions and still wasn't able to reproduce. Can you please maybe share a git repo with a script and the schema which reproduces this? I think it might be related to schema structure as well. |
What sort of schema structures would likely cause this ? BTW I've had to work around the issue by doing an explicit find / create / update so my code works as expected now. Even if I changed a value with the update (using the upsert command) e.g. the updated_at property to be new Date() it still behaved incorrectly (as mentioned in my previous post). |
We're also experiencing this issue in production with I can share my schema privately if it would help. |
also running into this issue (v3.8.1, mysql)...when creating a new entry upsert returns the expected data. if a record already exists matching there where it returns a count. however, if the record already exists and i use export async function login(body) {
let { email, name, image } = body
// the return statement below works as expected if a new record is created,
// otherwise if a matching record exists im just getting count back instead of what is in select
// like i do if its a new record created
return await db.user.upsert({
where: { email: email },
select: { id: true, name: true, image: true },
update: { name, image },
create: { email, name, image }
})
// if the record already exists and i use findUnique instead the correct information is
// returned instead of "count"
return await db.user.findUnique({
where: { email },
select: { id: true, name: true, image: true }
})
} here is the model its pulling from model User {
id String @id @default(cuid())
name String?
email String @unique
image String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
sessions Session[]
} |
I tried the above and still wasn't able to reproduce this. Can you please tell me the exact MySQL version you are using? |
@uvalus It might be schema parser which is creating the wrong query graph so I was asking for this. I still unable to trigger this so if you share a minimal example which can reproduce the problem consistently, please share it. |
@pantharshit00 this is on PlanetScale and it looks like they're using mysql version 8.0.23 |
Can confirm, I am using PlanetScale as well |
Also using Planetscale |
(Noice, seems we are getting somewhere here) |
Yes |
I can not reproduce this on PlanetScale with
For this code: const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
async function login(body) {
let { email, name, image } = body
return await prisma.user.upsert({
where: { email: email },
select: { id: true, name: true, image: true },
update: { name, image },
create: { email, name, image }
})
}
async function main() {
console.log("users before", await prisma.user.findMany({}))
let body1 = { email: "test", name: "test", image: "sf" }
let body2 = { email: "test", name: "123", image: "456" }
console.log("upsert1", await login(body1))
console.log("upsert2", await login(body2))
console.log("users after", await prisma.user.findMany({}))
}
main()
.catch((e) => {
console.error(e)
process.exit(1)
})
.finally(async () => {
await prisma.$disconnect()
}) and schema: generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
referentialIntegrity = "prisma"
}
model User {
id String @id @default(cuid())
name String?
email String @unique
image String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Am I missing something? @gt-codes Is your |
I can confirm I have the same issue. My configuration is for planetscale, but locally I'm using normal Mysql and can expeience the same problem |
So, yesterday I wasn't able to reproduce that behavior, as long as I tried. I checked with the same version I'm using (3.9.2). I'll keep trying but maybe @ItzDerock code is enough? |
Can other people as well here try the latest version and report back? |
So, here there's a reproduction repository based on @ItzDerock code but with the configuration for Mysql/Planet scale. |
Any news on this? |
We are still waiting for a confirmed reproduction. (Feel free to try the one posted in the comment above and let us know if this works - that will save us some time.) |
I provided a repository with the reproducible error a couple of comments above. Haven't you tried it? Did you try and it's working for you instead? |
Oh sorry, I missed that it was you who posted that - this way my message of course does not make a lot of sense. |
@janpio, I can confirm that the reproduction reproduces. Is it helpful? |
I can indeed confirm this as well now in the context of |
Can also confirm this; using PlanetScale on production but also happens locally with MySQL datasource db {
provider = "mysql"
url = env("DATABASE_URL")
referentialIntegrity = "prisma"
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity", "interactiveTransactions"]
} |
Any news on this? |
I'm unable to reproduce this issue on Prisma 4.1. Reopen please if you still run into the same issue after upgrading. |
I can confirm that this does not reproduce any more with 4.1:
Can you please confirm @ramiel @ponei @cdinu @ItzDerock etc? |
The issue still reproduces. Upsert, when updating is returning { count : number } |
Please open a new issue with a full reproduction if you still get this problem with a recent version of Prisma. We will not further investigate this in this closed issue from 2022. Thank you. |
I faced a similar problem. However, in my experience, the return format { count: number } occurred exclusively when the update object included the primary key. Works fine with unique keys. |
Running into the same issue using mysql. if it doesnt exist it returns the record if it exists it returns a count of updated records. Prisma ^5.9.1
|
Please open a new issue with a full reproduction if you still get this problem with a recent version of Prisma. We will not further investigate this in this closed issue from 2022. Thank you. |
Bug description
When using the
upsert
method to do afindOrCreate
implementation,count: { 1 }
is returned rather than the data itself.How to reproduce
Using the following code returns
count
rather than theuser
objectExpected behavior
I expect the user's data to be returned.
Prisma information
Environment & setup
Prisma Version
The text was updated successfully, but these errors were encountered: