question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`findMany` DateTime queries not finding applicable records

See original GitHub issue

Bug description

This bug was originally reported in https://github.com/RobertCraigie/prisma-client-py/issues/214 but was found to be an internal Query Engine issue as an equivalent Prisma Client query written in TypeScript yields the same unexpected behaviour.

How to reproduce

schema.prisma

// database
datasource db {
  provider = "sqlite"
  url      = "file:database.db"
}

// generator
generator client {
  provider = "prisma-client-js"
}

model Demo {
  id Int @id
  created_at DateTime @default(now())
}
  1. prisma db push
  2. Open sqlite: sqlite3 database.db
  3. Insert the record: INSERT INTO Demo (id) VALUES (1);
  4. Confirm the records existence: SELECT * FROM Demo;
  5. Run the script below
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

async function main() {
  const today = new Date();
  console.log(today);
  const records = await prisma.demo.findMany({
    where: {
        created_at: {
            lt: today,
        },
    },
  });
  console.log(records);
  console.log(await prisma.demo.findMany())
}

main()
  .catch((e) => {
    throw e;
  })
  .finally(async () => {
    await prisma.$disconnect();
  });

Output:

2022-01-08T11:09:09.035Z
[]
[ { id: 1, created_at: 2022-01-08T10:26:59.000Z } ]

There are more detailed tests included in the original reproduction: https://github.com/iiian/prisma_python_datefailure_demo/blob/master/demo.py

Expected behavior

The record should be found in the first query:

[ { id: 1, created_at: 2022-01-08T10:26:59.000Z } ]

Environment & setup

  • OS: Mac OS
  • Database: SQLite
  • Node.js version: v17.1.0

Prisma Version

prisma                  : 3.7.0
@prisma/client          : 3.7.0
Current platform        : darwin
Query Engine (Node-API) : libquery-engine 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine        : migration-engine-cli 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine    : introspection-core 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary           : prisma-fmt 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash    : 8746e055198f517658c08a0c426c7eec87f5a85f
Studio                  : 0.445.0

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
RobertCraigiecommented, Jan 9, 2022

Also should be noted that this bug does not occur when the record is created within Prisma, for example:

import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

function sleep(ms: number) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

async function main() {
  await prisma.demo.create({
    data: {
        id: 1,
    },
  });
  await sleep(2000);
  const today = new Date();
  console.log(today);
  const records = await prisma.demo.findMany({
    where: {
      created_at: {
        lt: today,
      },
    },
  });
  console.log(records);
  console.log(await prisma.demo.findMany());
}

main()
  .catch((e) => {
    throw e;
  })
  .finally(async () => {
    await prisma.$disconnect();
  });

Output:

2022-01-09T12:14:48.585Z
[ { id: 1, created_at: 2022-01-09T12:14:46.565Z } ]
[ { id: 1, created_at: 2022-01-09T12:14:46.565Z } ]

Using raw queries causes the original error:

- await prisma.demo.create({
-     data: {
-         id: 1,
-     },
-   });
+ await prisma.$queryRaw`INSERT INTO Demo (id) VALUES (1);`
1reaction
iiiancommented, Jan 9, 2022

Not an issue for MySQL.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prisma Client API (Reference)
findUniqueOrThrow retrieves a single data record in the same way as findUnique . However, if the query does not find a record, it...
Read more >
Prisma 2 query to return records only that are associated with ...
I created your model in Prisma 2 and used the following command to get a single principle that has the two tags associated...
Read more >
Date criteria doesn't work in my query - Microsoft Support
Getting unexpected or no results when you include date criteria in a query? Learn about some of the reasons why this happens.
Read more >
Query | GORM - GORM
Additionally, if no primary key is defined for relevant model, then the model will be ordered by the first ... This query would...
Read more >
GraphQL Search and Filter – How to search and filter results ...
Searching and filtering is a standard part of any GraphQL API. ... The following query asks for albums with the ids 1 ,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found