Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create() fails with DateTime type column as part of PRIMARY KEY in mssql server #10378

Open
shankuljain opened this issue Nov 21, 2021 · 3 comments
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: create() topic: dates / DateTime topic: sql server Microsoft SQL Server

Comments

@shankuljain
Copy link

shankuljain commented Nov 21, 2021

Bug description

When db table has datetime type column as part of primary key, create fails because datetime in Javascript contains milliseconds which are ignored by mssql. Now stored field (without milliseconds) doesn't match with the datetime with milliseconds hence the operation fails.

How to reproduce

Assume db table like which has datetime as PRIMARY KEY

create table test_table (
   time1 datetime not null PRIMARY KEY
)

and prisma insert operation like below -

await prisma.test_table.create(
  {
     data: {time1: new Date()}
  }
)

new Date contains millisecond information which is not supported by mssql server (or perhaps other databases as well). Now, mssql ignores these milliseconds and saves the time after setting milliseconds to zero. Now while trying to fetch the newly inserted record by the date (which contains milliseconds) it doesn't find the match hence rollback the transaction.

A quick hack to make it work for this is to have insert operation like below -

await prisma.test_table.create(
 {
    data: {time1: new Date(new Date().setMilliseconds(0))} // setting milliseconds to zero, so it matches at the time of fetching  newly inserted row.
 }
)

Expected behavior

Prisma should get rid of milliseconds and try to fetch field without milliseconds.

Prisma information

Environment & setup

  • OS: Linux
  • Database: MSSQL Server
  • Node.js version: 16

Prisma Version

3.4.2
@shankuljain shankuljain added the kind/bug A reported bug. label Nov 21, 2021
@janpio janpio added topic: datetime topic: sql server Microsoft SQL Server bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. team/client Issue for team Client. labels Nov 22, 2021
@pantharshit00 pantharshit00 added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. and removed bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. labels Jun 2, 2022
@pimeys pimeys self-assigned this Sep 2, 2022
@pimeys
Copy link
Contributor

pimeys commented Sep 5, 2022

This is correct. It even crashes when just creating a record with milliseconds, because we try to load it back from the database and cannot find any records.

@pimeys pimeys added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Sep 5, 2022
@pimeys pimeys removed their assignment Sep 5, 2022
@janpio
Copy link
Member

janpio commented Sep 9, 2022

2 similar cases for MySQL: #12783 + #8982

@martin31821
Copy link

I'm running into the same issue with a postgresql server, also with a Date as my primary key.

@janpio janpio changed the title Create fails with datetime type column as part of PRIMARY KEY in mssql server create() fails with DateTime type column as part of PRIMARY KEY in mssql server Feb 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: create() topic: dates / DateTime topic: sql server Microsoft SQL Server
Projects
None yet
Development

No branches or pull requests

5 participants