Skip to content

when quering, passing string to DateTime field results runtime error #5682

@umlx5h

Description

@umlx5h

Bug description

We can not pass string value to DateTime field even it allows passing string in type declaration.
So, type check is not working, and when executing it results following runtime error.

Argument <DateTime field>: Got invalid value '2021-01-01' on prisma.aggregatePost. Provided String, expected DateTimeFilter or DateTime.

I want to just pass string to datetime field in where clause and filtering and get the result.
It allows to pass string values to datetime field, and If we pass the string values, we get the runtime error.
I think it is a bug, If string type is not possible, it must be removed from type definition.

How to reproduce

prisma.schema

model Book {
  id          Int      @id @default(autoincrement())
  title       String
  publishedAt DateTime
}

main.ts

  // no type checking error
  // runtime error
  // Argument publishedAt: Got invalid value '2021-01-01' on prisma.createOneBook. Provided String, expected DateTime.
  await prisma.book.create({
    data: {
      title: "book",
      publishedAt: "2021-01-01",
    },
  });

  // no type checking error
  // runtime error
  // Argument publishedAt: Got invalid value '2021-01-01' on prisma.findManyBook. Provided String, expected DateTimeFilter or DateTime.
  await prisma.book.findMany({
    where: {
      publishedAt: "2021-01-01",
    },
  });

  // no type checking error
  // runtime error
  // Argument publishedAt: Got invalid value '2021-01-01' on prisma.deleteManyBook. Provided String, expected DateTimeFilter or DateTime.
  await prisma.book.deleteMany({
    where: {
      publishedAt: "2021-01-01",
    },
  });

Expected behavior

It should be simply allowed passing string values to DateTime field. because normal sql can do as follows.

INSERT INTO "Book"("title", "publishedAt") VALUES ('book', '2021-01-01');
SELECT * FROM WHERE publishedAt = '2021-01-01';

I don't want to convert string to date anything like that because it is not necessary in normal sql world.
and I don't want to convert and think about timezone.
It's like taking away the freedom of the user. so simply allowing any string or date object are appropriate I think.

Prisma information

Generated types allow passing string as follows.

index.d.ts

generated types export type BookWhereInput = { AND?: Enumerable OR?: Enumerable NOT?: Enumerable id?: IntFilter | number title?: StringFilter | string publishedAt?: DateTimeFilter | Date | string }

export type BookCreateInput = {
title: string
publishedAt: Date | string
}

export type BookUncheckedCreateInput = {
id?: number
title: string
publishedAt: Date | string
}

export type BookUpdateInput = {
title?: StringFieldUpdateOperationsInput | string
publishedAt?: DateTimeFieldUpdateOperationsInput | Date | string
}

export type BookUncheckedUpdateInput = {
id?: IntFieldUpdateOperationsInput | number
title?: StringFieldUpdateOperationsInput | string
publishedAt?: DateTimeFieldUpdateOperationsInput | Date | string
}

export type BookUpdateManyMutationInput = {
title?: StringFieldUpdateOperationsInput | string
publishedAt?: DateTimeFieldUpdateOperationsInput | Date | string
}

export type BookUncheckedUpdateManyInput = {
id?: IntFieldUpdateOperationsInput | number
title?: StringFieldUpdateOperationsInput | string
publishedAt?: DateTimeFieldUpdateOperationsInput | Date | string
}

Environment & setup

  • OS: WSL Ubuntu
  • Database: only tested in MySQL and PostgreSQL
  • Node.js version: v15.5.0
  • Prisma version: 2.16.1

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions