Allow shorter date format for native date type
See original GitHub issueBug description
If my PG table has a field of type Date
(https://www.postgresql.org/docs/9.1/datatype-datetime.html), when calling prisma introspect
this type will be converted to DateTime
in the prisma.schema
.
Later when I try to query the table through Prisma and send the value YYYY-MM-DD
to the related Date field, Prisma responds with the following error:
Got invalid value '2020-12-30' on prisma.findManyitems. Provided String, expected DateTimeFilter or DateTime._
If I try to wrap the date-string in new Date( , prisma converts the date to the following format:
YYYY-MM-DDT00:00:00.000Z`
However, when this new value is compared to the value that exists in the database, it is NEVER EQUAL due to the difference in the formats.
How to reproduce
- Create a new PostgreSQL table
CREATE TABLE public.hotels (
id serial NOT NULL,
checkin_date date NOT NULL
);
INSERT INTO public.hotels
(checkin_date)
VALUES ('2021-02-03');
- Introspect the database using: npx prisma introspect
- Notice the datatype of the checkin_date field in schema.prisma was converted to DateTime
- Executing the following query, will return an error.
const hotels = await this.prisma.hotels.findMany({
where: {
checkin_date: '2021-02-03'
}
});
- Executing the following will return no results:
const hotels = await this.prisma.hotels.findMany({
where: {
checkin_date: new Date('2021-02-03')
}
});
Workaround
const hotels = await this.prisma.hotels.findMany({
where: {
checkin_date: '2021-02-03' + 'T00:00:00.000Z'
}
});
Expected behavior
Prisma should have matched the date to the one in the database and return the record.
Please add support in Prisma for both Date and Time PostgreSQL data types
Environment & setup
- OS: Debian
- Database: PostgreSQL
- Node.js version: 14.13.1
- Prisma version:
@prisma/cli : 2.11.0
@prisma/client : 2.9.0
Issue Analytics
- State:
- Created 3 years ago
- Reactions:35
- Comments:18 (4 by maintainers)
Top GitHub Comments
100% agree with @carchrae that there should be support for a ādate without timeā type. In Postgres, this is the
Date
data type. The intent is to store a date that is agnostic to time zone (e.g.,"2021-05-05"
).Not being able to simply use this string as an input makes life a lot harder and causes uncertainty. Itās hard to feel confident about what will happen if I insert a date using
new Date("2021-05-06")
because the resulting Date object will get assigned the time zone of the machine/server running the code.Will Prisma simply leave the year, month, and day component alone and stick that in the database, or will it try to convert to UTC (or something else)? I donāt know, and thatās the problem. Maybe itās documented somewhere, but itās a burden to even have to think about issues like this. If we could just insert
"2021-05-06"
as Postgres intended, it would bypass all of these issues.@matthewmueller
@db.Time
should accept a time as string too.e.g.