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

DateTime precision loss leads to records being unable to be found #129

Closed
RobertCraigie opened this issue Nov 17, 2021 · 0 comments · Fixed by #162
Closed

DateTime precision loss leads to records being unable to be found #129

RobertCraigie opened this issue Nov 17, 2021 · 0 comments · Fixed by #162
Labels
bug/2-confirmed We have confirmed that this is a bug. kind/bug A reported bug.
Projects
Milestone

Comments

@RobertCraigie
Copy link
Owner

Bug description

Internally, Prisma truncates DateTime values to 3 decimal places, e.g.

2021-11-17 17:04:56.936198 -> 2021-11-17 17:04:56.936000

This can lead to a bug where records cannot be filtered by a DateTime equals value.

This bug was not caught until now as it does not appear on SQLite but does on PostgreSQL.

How to reproduce

import asyncio
from datetime import datetime
from prisma import Client

async def main() -> None:
    db = Client()
    await db.connect()

    date = datetime.utcnow()
    print(date)

    post = await db.post.create(
        data={
            'title': 'My first post',
            'created_at': date,
        },
    )
    print(post.created_at)

    found = await db.post.find_first(
        where={
            'created_at': date,
        },
    )
    print(f'Post found? {found is not None}')

    await db.disconnect()

if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())

SQLite output:

2021-11-17 17:13:21.288983 
2021-11-17 17:13:21.288000+00:00 
Post found? True

PostgreSQL output:

2021-11-17 17:14:15.055458 
2021-11-17 17:14:15.055000+00:00 
Post found? False

Expected behavior

PostgreSQL output:

2021-11-17 17:14:15.055458 
2021-11-17 17:14:15.055000+00:00 
Post found? True

Prisma information

datasource client {
  // provider = "sqlite"
  // url      = "file:dev.db"
  provider = "postgres"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-py"
  interface = "asyncio"
}

model Post {
  id         String   @id @default(cuid())
  created_at DateTime @default(now())
  updated_at DateTime @updatedAt
  title      String
  published  Boolean  @default(false)
  desc       String?
}

Environment & setup

  • OS: Mac OS
  • Database: PostgreSQL
  • Python version: Python 3.9.1
  • Prisma version:
prisma               : 3.4.0
prisma client python : 0.3.0
platform             : darwin
engines              : 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85
install path         : .venv/lib/python3.9/site-packages/prisma
installed extras     : []
@RobertCraigie RobertCraigie added bug/2-confirmed We have confirmed that this is a bug. kind/bug A reported bug. labels Nov 17, 2021
@RobertCraigie RobertCraigie added this to To do in v1.0.0 via automation Nov 17, 2021
@RobertCraigie RobertCraigie added this to the v0.3.2 milestone Nov 19, 2021
@RobertCraigie RobertCraigie added the process/candidate Candidate for the next release label Nov 28, 2021
v1.0.0 automation moved this from To do to Done Dec 4, 2021
@RobertCraigie RobertCraigie removed the process/candidate Candidate for the next release label Dec 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed We have confirmed that this is a bug. kind/bug A reported bug.
Projects
Development

Successfully merging a pull request may close this issue.

1 participant