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.

Create a method for the schema attribute `@updatedAt` to have a default value of `null`

See original GitHub issue

Problem

We would like a way to have a column in a table with the attribute @udpatedAt to have a default of null for data clarity.

Example schema.prisma:

model User {
  id            Int       @id @default(autoincrement())
  email         String    @unique
  name          String
  createdAt     DateTime  @default(now())
  updatedAt     DateTime? @updatedAt
}

Suggested solution

Allow the schema attribute @updatedAt to be used with @default(null).

Suggested example schema.prisma:

model User {
  id            Int       @id @default(autoincrement())
  email         String    @unique
  name          String
  createdAt     DateTime  @default(now())
  updatedAt     DateTime? @updatedAt @default(null)
}

Alternatives

Change the default value of @updatedAt to be null and allow @default(now) to be used. This suggestion could potentially be a breaking change for projects so I think the first suggestion is ideal.

Additional context

We’re aiming to make columns not dependant on others as much as possible and want a way to tell if a column has been updated without comparing the createdAt and updatedAt columns. @austincrim also mentioned there may be bugs down the road in this discussion: https://github.com/prisma/prisma/discussions/11559.

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
francismanansalacommented, May 13, 2022

@janpio Thanks for looking into this feature request. The feature my team is wanting is when the record is first created with the value for updatedAt column is NULL, and then on any updates to the record updatedAt to be now()

2reactions
janpiocommented, May 13, 2022

Not yet, or there would already have been a feature request for this. Generally I think it is a valid use case though - it is just a slightly different behavior than what we implemented.

The quick fix would be to add an optional paramter to the @updatedAt attribute, e.g. @updatedAt(nullDefault) or something like that - but the @updatedAt feature has some other problems as well - see https://github.com/prisma/prisma/issues?q=is%3Aopen+label%3A"topic%3A+updatedAt"+sort%3Aupdated-desc - so I don’t think we will just want to quickly add this but consider the bigger feature.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How Do I Fix this Sequelize Database Error when trying to ...
You have 2 problems: 1) At your SQL declaration and in your model declaration you're missing the default value for the title column....
Read more >
Schema Incompatibilities | PostgreSQL
There's no DEFAULT constraint added to the database column. Because this constraint is not reflected in the database itself, the Prisma 2.x and...
Read more >
Schema
You can set a default value for an attribute that will be applied upon save if the given attribute value is null or...
Read more >
Specify default column values | BigQuery
The write stream schema contains the field b , so the default value default_b is not used even when no value is specified...
Read more >
Model definition
BOOLEAN, allowNull: false, defaultValue: true }, // default values for dates ... By default, Sequelize will add the attributes createdAt and updatedAt to ......
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