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

default(now()) on the same table sometimes yield different times #13795

Closed
DudeRandom21 opened this issue Jun 13, 2022 · 1 comment · Fixed by prisma/prisma-engines#3005
Closed
Assignees
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client.
Milestone

Comments

@DudeRandom21
Copy link

Bug description

If there are two columns in the same table both with @default(now()) they may get slightly different times. We noticed because we have a table that has both a created datetime and an updated datetime field which are both defaulted to now at create time, but we have a constraint that updated_dt >= created_dt just as a sanity check.

I suspect this may be related to or have the same underlying cause as #12572.

How to reproduce

This is a bit tricky to reproduce given that it's a race condition.

  1. Create a table with two DateTime columns with @default(now()) and using the highest level of precision.
  2. Add a constraint that one column must be >= to the other (or some other method of detecting the issue).
  3. Insert a row
  4. Repeat step 3 until you get a failure.

Expected behavior

Any number of @default(now()) columns should all get exactly the same timestamp.

Prisma information

Here's a minimal example sufficient to see the bug
Prisma schema

model foo {
  id         Int       @id @default(autoincrement())
  created_dt DateTime? @default(now()) @db.Timestamp(6)
  updated_dt DateTime? @default(now()) @db.Timestamp(6)
}

And a test that should see the issue occurring

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

let prismaClient = new PrismaClient();

const test = async () => {
  const f = await prismaClient.foo.create({
    data: {},
  });
  expect(f.updated_dt).toEqual(f.created_dt);
};

for (let i = 0; i < 100; i++) {
  it(`should insert a foo row: ${i}`, test);
}

On my system I see the issue about 3% or so of the time so 100 repetitions should be enough to see it, but you may need to increase the number of iterations in the loop.

Environment & setup

  • OS: Ubuntu 20.04 LTS
  • Database: PostgreSQL 13
  • Node.js version: 14.17.6

Prisma Version

Environment variables loaded from .env
prisma                  : 3.14.0
@prisma/client          : 3.14.0
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine 2b0c12756921c891fec4f68d9444e18c7d5d4a6a (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli 2b0c12756921c891fec4f68d9444e18c7d5d4a6a (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core 2b0c12756921c891fec4f68d9444e18c7d5d4a6a (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt 2b0c12756921c891fec4f68d9444e18c7d5d4a6a (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : 2b0c12756921c891fec4f68d9444e18c7d5d4a6a
Studio                  : 0.460.0
@DudeRandom21 DudeRandom21 added the kind/bug A reported bug. label Jun 13, 2022
@DudeRandom21 DudeRandom21 changed the title default(now()) on the same field sometimes yield different times default(now()) on the same table sometimes yield different times Jun 13, 2022
@matthewmueller matthewmueller added team/client Issue for team Client. bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Jun 13, 2022
@Weakky Weakky self-assigned this Jun 23, 2022
@Weakky Weakky added this to the 4.0.0 milestone Jun 23, 2022
@Weakky
Copy link
Member

Weakky commented Jun 27, 2022

Hey,

This issue was just fixed and will be available in the next release (4.0).

Thanks for reporting 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants