prisma-client-py icon indicating copy to clipboard operation
prisma-client-py copied to clipboard

Datetime microsecond precision dropped by query builder

Open Oblynx opened this issue 1 year ago • 6 comments

Bug description

I have a model with a column of type DateTime @db.timestamptz(6) as primary key. Microsecond precision is essential in this application. I try to create multiple rows with the same millisecond but different microseconds and Prisma throws a UniqueViolationError.

How to reproduce

TODO: complete MVE

With this model:

model MetadataMarkers {
  metadataID   BigInt
  timestamp     DateTime              @db.Timestamptz(6)
  marker  Json
  @@id([metadataID, timestamp])

Expected behavior

All the rows are added successfully.

Environment & setup

  • OS: Fedora 37
  • Database: Postgresql
  • Python version: 3.11.4
  • Prisma version:
prisma                  : 4.15.0
prisma client python    : 0.9.1
platform                : debian-openssl-1.1.x
expected engine version : 8fbc245156db7124f997f4cecdd8d1219e360944
installed extras        : []
install path            : /microservices/workspace/.venv/lib/python3.11/site-packages/prisma
binary cache dir        : /home/vscode/.cache/prisma-python/binaries/4.15.0/8fbc245156db7124f997f4cecdd8d1219e360944

Oblynx avatar Aug 21 '23 17:08 Oblynx

After some digging, I can locate the issue at the prisma QueryBuilder. In this screenshot, on the debugger pane (right), you see that the QueryBuilder arguments have microsecond precision. However, the builder.build() only has millisecond precision.

image

Oblynx avatar Aug 21 '23 17:08 Oblynx

Digging deeper, it turns out that the precision is lost when rendering the Data node of the builder. Compare the timestamp in the fields children and data.

image

Oblynx avatar Aug 21 '23 18:08 Oblynx

The root cause is the fix of this older issue: https://github.com/RobertCraigie/prisma-client-py/issues/129

Oblynx avatar Aug 21 '23 18:08 Oblynx

IIUC, #129 was a workaround when Prisma didn't support microsecond precision in the field type definition. However, it now does. Therefore, I propose removing this old behavior.

Oblynx avatar Aug 21 '23 18:08 Oblynx

Ah awesome, I was not aware Prisma now supports microsecond precision.

Thanks for investigating and opening a PR!

RobertCraigie avatar Aug 22 '23 15:08 RobertCraigie

I hope to keep them coming! I'm really happy to use Prisma from python at work <3

Oblynx avatar Aug 22 '23 15:08 Oblynx