prisma: `now()` as a value for DateTime fields since `new Date()` may be wrong

Problem

There is no reliable way to update a DateTime field to now(). @default(now()) only works for create. Assume the db runs on a server with a precise clock, but the query comes from another device (server, browser) whose clock you have no control over. If you use new Date() to update, it evaluates to this potentially wrong clock which is not in sync with the clock of the db. This may lead to broken invariants like time intervals that end (new Date()) before they start (now()).

Suggested solution

DateTime could also allow now() as a string literal to pass through to the db (or change to the db’s version of now()).

Alternatives

  • executeRaw
  • introduce a dummy field to update and rely on @updatedAt, see discussion below - seems like a hack/does not reflect intent.

Additional context

Example in discussion https://github.com/prisma/prisma/discussions/5582.

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 96
  • Comments: 16

Most upvoted comments

Any update on this? I think it is fairly common that in most of situations we do want to take the server time as the client time is not trustable.

It’s 2023 and people still think commenting “+1” is appropriate.

No, it’s spamming everyone that is subscribed to this issue. Please stop commenting “+1” and simply upvote the issue instead.

Even this comment is meta and spams subscribers, so, sorry about that.

I’m also very interested in this discussion. It’s really important in any context where the API code is not residing in the same server/timezone/place/universe as the DB.

Here, we ended up by using the raw execution alternative solution, like:

prisma.$executeRaw(`UPDATE "Invite" SET "acceptedAt" = CURRENT_TIMESTAMP WHERE "id" = '${invite.id}'`)

This is not elegant in any way. It requires some SQL knowledge that maybe not all the team members have. String manipulation is always more prone to human errors and… Well, why not a Prisma solution?

But the problem is: if we start to accept any string as valid type for date fields, we also break some type checking advantages. Prisma should not accept every string as a valid type for a date field.

So Prisma could provide some kind of constants. Maybe something like:

prisma.invite.update({
  where: { id: invite.id },
  data: { acceptedAt: prisma.now }
})

Or a more powerful solution could be allowing a dbgenerated function as a valid type. It would even allow calling DB functions during our create/update operations, like:

prisma.invite.update({
  where: { id: invite.id },
  data: {
    someUuidField: prisma.dbgenerated('gen_random_uuid()'),
    acceptedAt: prisma.dbgenerated('now_plus_60_minutes()')
  }
})

I have an endpoint that start and stops a synchronized but low-resolution timer, and was surprised to see that Prisma doesn’t have a solution for this. Being able to use SQL now() in this context would be exceedingly helpful

2024 … no support for now() … finally found something more important that geospatial support. is Prisma fit for production environments?

It’s common that the app server would not be the same box / VM as the DB server. The DB server is often the best choice for a stable time base. The difference would often be negligible (for most apps) in a production environment but can be far off in dev-test situations and especially when running the app on the dev’s box. Some solution for this plus a short article in the Prisma docs would help devs recognize the issue and do the right thing more often.

I don’t really want to start a beef with you but you should probably lean on what your profile says and learn how to contribute and behave on the open source community. Quick link for fun.

image

Not that I don’t like jokes, but even your second comment irritates me with useless notifications. I’ll stop commenting now as I don’t help with the issue. Thanks for the gif I guess and have a great day!

Any update?

I know, but that’s another issue. My point was that it’s not possible to call user-defined functions and be type-safe without also listing those functions (and their type) in the schema.

Typescript has string literal types, so you’d just have something like Date | "now()" instead of Date. This could be used for any DB function (as long as codegen is aware of it - idk where your now_plus_60_minutes() should come from).

+1

Any updates? That should be a basic task for any ORM