prisma: Error: Provided String, expected DateTime or DateTimeFieldUpdateOperationsInput
I’m not entirely sure if this is a bug! If not a bug, then I’m a bit confused with the DateTime type…
Using Prisma 2.11.0 (with MySQL), I’ve created a new DateTime field in my schema.prisma
file as following:
model Story {
lastSavedAt DateTime @default(now())
}
When trying to update this particular field with the value 2020-11-23T17:41:59+11:00
I’m getting back the following error:
Argument lastSavedAt: Got invalid value '2020-11-23T17:41:59+11:00' on prisma.updateOneStory. Provided String, expected DateTime or DateTimeFieldUpdateOperationsInput:
type DateTimeFieldUpdateOperationsInput {
set?: DateTime
}
type DateTimeFieldUpdateOperationsInput {
set?: DateTime
}
The error outlines that I can’t use a String, while the TypeScript type returned for lastSavedAt
is:
// From VSCode
lastSavedAt?: string | Date | DateTimeFieldUpdateOperationsInput
// In the node_modules/.prisma/client/index.d.ts file
export type StoryUpdateInput = {
lastSavedAt?: XOR<Date | string, DateTimeFieldUpdateOperationsInput>
}
Am I missing something?
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 4
- Comments: 15 (4 by maintainers)
Commits related to this issue
- fix(client): date parsing Closes #4328 — committed to prisma/prisma by timsuchanek 3 years ago
Thanks for jumping on this @pantharshit00. Just to be clear, in my example above I was already using an ISO 8601 DateTime format - though allow me to expand a bit more around the issue.
I’ve been running a few different tests since my last message and here is what I’ve found:
.toISOString()
is working as expected.new Date()
is working as expected.Here’s a sandbox showing the issue using date-fns/formatISO (open terminal > yarn run test): https://codesandbox.io/s/prismadatetimedatefns-l7i2u?file=/index.ts
For my use case, I’m using Prisma inside a Lambda function, in combination with AWS AppSync. Therefore, my DateTime inputs are coming through as AWSDateTime scalar type (ISO 8601). In that context, you will understand that using
.toISOString()
ornew Date()
isn’t easily applicable. Ideally, Prisma would accept the ISO 8601 returned by AWS AppSync.Happy to be proven wrong, but I feel like Prisma isn’t supporting the ISO 8601 format entirely. But only in the exact shape returned by
.toISOString()
?Am also facing this problem, the workaround that worked for me is to convert the string to date with
new Date(...)
or you just convert your value to.toISOString()
Should be simple to deal with native
Date
objects without having to deal withDateTime
object and timezone constraints. Most of the ORMs support passing just add-mm-yyyy
oryyyy-mm-dd
for that, so no issues with timezones.This issue is opened for over 3 years, so we need to understand that Prisma team might be busy with other high-priority tasks - is not easy to maintain an amazing open source library for this - so massive thanks.
For whoever is looking for alternatives - we had to mess with timezones (
TZ
env variable, or convert thedd-mm-yyyy
to date for specific timezones and we had problems), so we have migrated all queries that deal with that to use https://github.com/drizzle-team/drizzle-orm - it has a fantastic type support and developer experience and it’s useful to deal with cases where prisma doesn’t support native stuff.Sorry for the late reply here.
@navicstein
If you want to filter via dates, you will need to use the date native type. By default
DateTime
maps totimestamp(3)
in the database which has millisecond precision in match and storing. If you just want to filter, use theDate
native type like so(native types are in preview):Now regarding your issue @maoosi. I can confirm that as a bug in our JS validator for dates.
Here is our date handling code: https://github.com/prisma/prisma-engines/blob/9a901beeaa4d52b23483c02dc9803aadb56f0e28/libs/prisma-value/src/lib.rs#L208 . This looks alright but the JS code doesn’t seem to have a proper validator. I think our regular expression is wrong here: https://github.com/prisma/prisma/blob/557aaaddae6c826c0b01fa282d0a28df2fa1011d/src/packages/client/src/runtime/utils/common.ts#L195
Reproduction:
@maoosi @pantharshit00 Am not sure that Prisma’s date-time thingy is working as expected, let’s assume I have some data like this
Then my query is
_date
should at least match the last two objects in the above result since is still in the range of2020-12-04
but instead is empty[]
but… if I omit.toISOString()
, the entire results will be fetched which is not desired, changing the Prisma type fromDateTime
toString
and using the queryseems to solve this problem, but why’s Prisma looking up
DateTime
in strict mode? at leastThanks a lot for reporting 🙏 This issue is fixed in the latest dev version of
@prisma/cli
. You can try it out withnpm i -g @prisma/cli@dev
.In case it’s not fixed for you - please let us know and we’ll reopen this issue!
Hey @maoosi
This is intended. Prisma will only accept DateTime in ISO 8061 format(https://en.wikipedia.org/wiki/ISO_8601).
If you want use another format, you can should parse it and convert it into a javascript Date object as suggested @navicstein. We will then internally call
toISOString()
on that Date object and pass it to the database.Awesome, thanks!