'now()' as a value for DateTime fields since `new Date()` may be wrong
See original GitHub issueProblem
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:
- Created 3 years ago
- Reactions:27
- Comments:8
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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.
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