[Atomic number operations] I have a schema that ha...
# orm-help
m
[Atomic number operations] I have a schema that has an optional Int field
tokenGenerationCount
that I need to increment +1 every time logic generates the
access_token
, but it doesn’t seems to work. Below is the schema:
model userSessionInfo {
Copy code
id                   String @id @default(cuid())
userId               String @unique
deviceSignature      String @unique
...
tokenGenerationCount Int @default(0)
}
And here’s the code to update:
const updateTokenCount = await prisma.userSessionInfo.update({
where: {
id: “sh3883ejJdiwoQ”
},
data: {
tokenGenerationCount: {
increment: 1
}
}
})
It fails to increment
tokenGenerationCount
without any error. The example in the doc only provides sample code for
updateMany
method:
Copy code
const updatePosts = await prisma.post.updateMany({
  data: {
    views: {
      increment: 1,
    },
    likes: {
      increment: 1,
    },
  },
})
r
@Martïn 👋 This works fine for me on Prisma version 2.16.0 I tried it with the following query:
Copy code
const session = await prisma.userSessionInfo.create({
    data: { deviceSignature: 'ds', userId: 'uid' },
  })
  console.log(session)

  const updatedSession = await prisma.userSessionInfo.update({
    where: {
      id: session.id,
    },
    data: {
      tokenGenerationCount: {
        increment: 1,
      },
    },
  })
  console.log(updatedSession)
And here’s the output:
m
@Ryan I finally got it working. Thanks for your help. Could you please try also this modification I made to your code: I added
deviceSignature
to the
where
argument
Copy code
const updatedSession = await prisma.userSessionInfo.update({
    where: {
      id: <http://session.id|session.id>,
      deviceSignature: ‘ds’
    },
    data: {
      tokenGenerationCount: {
        increment: 1,
      },
    },
  })
  console.log(updatedSession)
I only want to update based on the user device agent string or deviceType in my case.
r
In this case, you would need to use
updateMany
as you are specifying multiple arguments.
update
only expects a single unique argument in the
where
clause. You can read more about that here 🙂
m
@Ryan Here’s my use case: UPDATING A SINGLE ROW THAT MEETS THE "WHERE" MULTIPLE ARGUMENT QUERY: I have a dataset with columns having a
userId
,
country
,
email
,
profileId
and `rating.`Now I want to update the
rating
field in just one row that matches this condition where the
userId
is “Ryan”,
email
is “ryan@prisma.io” AND
company
is “Prisma” only. So
userId
must be “Ryan” and company must be “Prisma” to be able to update the
rating
field. We can have multiple users like Nikolas working also for Prisma, but only Ryan meets this condition. Somewhere in the doc (Combining operators) listed OR and NOT. Something like AND is needed in this case if not already in view:
const result = await prisma.user.update({
where: {
AND:[
{ userId: "ryan"},
{ email: "<mailto:ryan@prisma.io|ryan@prisma.io>" },
{ company: "Prisma" }
]
},
data: {
rating: {
increment: 1
}
})
MongoDB supports this operations for single and multi-row updates like this:
db.user.find(
{
"_id" : "ryan",
"email":"<mailto:ryan@prisma.io|ryan@prisma.io>",
"company": "Prisma"} ,
{
$inc:
{ rating: 1 }
}
)
@Ryan Never mind, I found the AND/OR operators deep in the doc...exactly what I needed.
💯 1