Create a method for the schema attribute `@updatedAt` to have a default value of `null`
See original GitHub issueProblem
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:
- Created 2 years ago
- Reactions:9
- Comments:11 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@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
updatedAtcolumn isNULL, and then on any updates to the recordupdatedAtto benow()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
@updatedAtattribute, e.g.@updatedAt(nullDefault)or something like that - but the@updatedAtfeature 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.