prisma-client-py
prisma-client-py copied to clipboard
Datetime microsecond precision dropped by query builder
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
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.
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
.
The root cause is the fix of this older issue: https://github.com/RobertCraigie/prisma-client-py/issues/129
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.
Ah awesome, I was not aware Prisma now supports microsecond precision.
Thanks for investigating and opening a PR!
I hope to keep them coming! I'm really happy to use Prisma from python at work <3