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.

`createdAt @default(now())` and `updatedAt @updatedAt` get different times on row creation

See original GitHub issue

Bug description

I have standard table like this:

model User {
  id                     String              @id @default(cuid())
  createdAt              DateTime            @default(now())
  updatedAt              DateTime            @updatedAt
}

I always assumed that after a prisma.user.create({}), the createdAt and updatedAt would be the same dates, matching to the millisecond. I’m seeing that’s not the case and updatedAt many times has a few extra millisecond(s).

How to reproduce

  1. Create the table above
  2. Add many rows and log the query result
  3. You’ll see what I describe eventually

Expected behavior

On creation, createdAt and updatedAt are always identical. I suppose if @updatedAt kind of columns would also default to CURRENT_TIMESTAMP like the default(now()) do, the problem would be resolved

Prisma information

The schema above is enough

Environment & setup

  • OS: Ubuntu 20
  • Database: PostgreSQL
  • Node.js version: v14.17.0

Prisma Version

prisma                  : 3.6.0
@prisma/client          : 3.6.0
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine dc520b92b1ebb2d28dc3161f9f82e875bd35d727 (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli dc520b92b1ebb2d28dc3161f9f82e875bd35d727 (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core dc520b92b1ebb2d28dc3161f9f82e875bd35d727 (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt dc520b92b1ebb2d28dc3161f9f82e875bd35d727 (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : dc520b92b1ebb2d28dc3161f9f82e875bd35d727
Studio                  : 0.440.0
Preview Features        : filterJson

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
tomhoulecommented, Sep 21, 2022

This will be fixed in next release. The relevant PR is https://github.com/prisma/prisma-engines/pull/3200

3reactions
okomarovcommented, Jul 13, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nexus Prisma - How to handle createdAt and updatedAt ...
When using @default(now()) and @updatedAt set 2 differents timestamp when created, determine if a record have been updated using createdAt !==
Read more >
Schema Incompatibilities | PostgreSQL
Manually add the @default(now()) attribute to the Prisma model ... If the @updatedAt attribute is set in the Prisma schema and you run...
Read more >
11.2.5 Automatic Initialization and Updating for ...
An auto-updated column is automatically updated to the current timestamp when the value of any other column in the row is changed from...
Read more >
Not respecting DEFAULT CURRENT_TIMESTAMP(6).
and the column `updatedAt` has a default of. NULL ON UPDATE CURRENT_TIMESTAMP . The `addedAt` column should not have an ON UPDATE clause...
Read more >
Adding createdAt and updatedAt Timestampable Fields
Let's do more magic! I always like to have createdAt and updatedAt fields on my database tables. A lot of times, this helps...
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