Repository navigation
How to deal with createdAt and updatedAt field in a prisma model?
#20323
|
I use sequelize generated the model: npx sequelize model:generate --name DispsysProj --attributes proj_name:string,proj_dir:string,proj_version:string,proj_desc:text In the schema.prisma, it is this: model DispsysProjs {
id Int @id @default(autoincrement())
proj_name String? @db.VarChar(255)
proj_dir String? @db.VarChar(255)
proj_version String? @db.VarChar(255)
proj_desc String?
createdAt DateTime @db.Timestamptz(6)
updatedAt DateTime @db.Timestamptz(6)
}I have a question about it, to my way of thinking, the createdAt? DateTime @db.Timestamptz(6)
updatedAt? DateTime @db.Timestamptz(6) |
Replies: 2 comments 2 replies
|
Hello @liaodalin19903 👋 You would need to use the The The Your model would look something like this: model DispsysProjs {
id Int @id @default(autoincrement())
proj_name String? @db.VarChar(255)
proj_dir String? @db.VarChar(255)
proj_version String? @db.VarChar(255)
proj_desc String?
createdAt DateTime @default(now()) @db.Timestamptz(6)
updatedAt DateTime @updatedAt @db.Timestamptz(6)
}With these changes, you should not need to provide values for createdAt and updatedAt when you create or update records in the DispsysProjs table. Prisma will manage these fields for you. Please let us know in case you have any other queries. |
|
Guys I just found something interesting about |

Hello @liaodalin19903 👋
You would need to use the
@defaultand@updatedAtattributes in your Prisma model.The
@defaultattribute sets a default value for a field each time a new record is created. You can use it withnow()to set the current timestamp as the default value. (Reference)The
@updatedAtattribute updates the timestamp each time the record is updated. (Reference)Your model would look something like this: