Prisma Query with Date

Viewed 383

Suppose I have table in Postgres SQL with prisma.

// Prisma Model

model my_table {
    comment   String   @db.VarChar(255)
    date      DateTime @db.Timestamp(6) // Stored as Now() in sql
}

After insert into my table I have. (the date is stored with no timezone)

// SQL DB Table

| comment  |           date             |
| -------- | -------------------------- |
| Hello    | 2022-08-04 08:16:32.716904 |

I want to query here

// Prisma Query

const rows = await this.prismaClient.my_table.findMany({
     where: {
        date: new Date('2022-08-04T08:16:32.716904')
     },
};

But for some reason I get back no records at all

I have also tried with:


new Date('2022-08-04T08:16:32.716904Z')
new Date('2022-08-04T08:16:32.716Z')
new Date('2022-08-04T08:16:32')
new Date('2022-08-04T08:16:32.716Z').toUTCString()
new Date('2022-08-04T08:16:32.716Z').toISOString()

but none seems to work, any suggestions or help will be appreciated.

1 Answers

I have tested, but all works fine. I guess, there is no need to convert string to date. Just try to send YY-MM-DD format date

const rows = await this.prismaClient.my_table.findMany({
     where: {
        date: yourDate       // where yourDate=2022-08-20
     },
}

If it won't help, change type of date to @db.Date()

Another way is using raw query with "like" operator

Related