question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:27
  • Comments:8

github_iconTop GitHub Comments

9reactions
leoyli-headsupcommented, Aug 24, 2021

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.

4reactions
zenhobcommented, Aug 16, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django datetime issues (default=datetime.now())
now() is evaluated when the class is created, not when new record is being added to the database. To achieve what you want...
Read more >
Demystifying DateTime Manipulation in JavaScript - Toptal
In JavaScript, a time stamp is the number of milliseconds that have passed since January 1, 1970. If you don't intend to support...
Read more >
Date and datetime fields formatting and wrong data type issue ...
Hello,I'm trying to update a field in the Potentials module;update a datetime field with a date field value. But, I'm getting the error...
Read more >
adding 1 year to a date field is showing wrong value.
But when i open the record and check the field value it showing wrong. (Highlighted field). ... var gd = new GlideDate(); gd.setValue(date);...
Read more >
Date() constructor - JavaScript - MDN Web Docs - Mozilla
Note: Date() can be called with or without new , but with different effects. See Return value. Parameters. There are five basic forms...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found