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.

Allow shorter date format for native date type

See original GitHub issue

Bug 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

  1. 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');
  1. Introspect the database using: npx prisma introspect
  2. Notice the datatype of the checkin_date field in schema.prisma was converted to DateTime
  3. Executing the following query, will return an error.
const hotels = await this.prisma.hotels.findMany({
      where: {
            checkin_date: '2021-02-03'
       }
});
  1. 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:open
  • Created 3 years ago
  • Reactions:35
  • Comments:18 (4 by maintainers)

github_iconTop GitHub Comments

14reactions
devuxercommented, May 6, 2021

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.

12reactions
vimutti77commented, Oct 4, 2021

@matthewmueller @db.Time should accept a time as string too.

e.g.

const store = await prisma.store.create({
  data: {
    openTime: '10:00:00',
    closeTime: '20:00:00',
  }
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Format a date the way you want - Microsoft Support
But you can change the date to be shorter or longer. To see a short date like 2/2/2013, select the cell, and then...
Read more >
Is there any way to change input type="date" format?
You have changed the input format of the moment function and it is not the way to go. You should have changed data-date-format="DD-YYYY-MM"...
Read more >
How to Format Dates in JavaScript with One Line of Code
Let's take a look at two methods that you can use to format your dates in the best way so you can use...
Read more >
Date formats - W3C
Use a locale neutral format; Make the month and year obvious; Use the Accept-Language HTTP header. Option One: Use a locale neutral format....
Read more >
Intl.DateTimeFormat() constructor - JavaScript - MDN Web Docs
The Intl.DateTimeFormat() constructor creates Intl.DateTimeFormat objects that enable language-sensitive date and time formatting.
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