this's my datebase column info;
`create_time` datetime DEFAULT NULL COMMENT 'Create Time',
`update_time` datetime DEFAULT NULL COMMENT 'Update Time',
and this's my entity
@CreateDateColumn({ type: 'datetime', name: 'create_time' })
createTime: Date
@UpdateDateColumn({ type: 'datetime', name: 'update_time' })
updateTime: Date
and this's my save code
const userRepo = this.userRepository.create(userDto);
console.log('Before save:', userRepo);
const user = await this.userRepository.save(userRepo);
console.log('After save:', user);
Using the @CreateDateColumn column, typeORM should automatically write to the data database & table, but the current write execution does not automatically handle the data write of this createTime.
console result
Before save: UserEntity {
nickName: 'me@mail.sslpool.cn',
email: 'me@mail.sslpool.cn',
passwd: '123456'
}
query: START TRANSACTION
query: INSERT INTO `sp_user`(`id`, `user_code`, `nick_name`, `email`, `passwd`, `status`, `create_time`, `update_time`) VALUES (DEFAULT, ?, ?, ?, ?, DEFAULT, DEFAULT, DEFAULT) -- PARAMETERS: ["61b717767173","me@mail.sslpool.cn","me@mail.sslpool.cn","$2a$10$Q2SstVvq.qBTH0Y8AT6nDegH5z8RNdbQV/v8kPOB1H3d5F9zKj4XW"]
query: SELECT `UserEntity`.`id` AS `UserEntity_id`, `UserEntity`.`create_time` AS `UserEntity_create_time`, `UserEntity`.`update_time` AS `UserEntity_update_time` FROM `sp_user` `UserEntity` WHERE `UserEntity`.`id` = ? -- PARAMETERS: [1]
query: COMMIT
After save: UserEntity {
nickName: 'me@mail.sslpool.cn',
email: 'me@mail.sslpool.cn',
passwd: '$2a$10$Q2SstVvq.qBTH0Y8AT6nDegH5z8RNdbQV/v8kPOB1H3d5F9zKj4XW',
userCode: '61b717767173',
id: 1,
createTime: null,
updateTime: null
}
[Nest] 31124 - 2024/07/19 11:15:58 DEBUG [UserService] user completed => {"nickName":"me@mail.sslpool.cn","email":"me@mail.sslpool.cn","passwd":"$2a$10$Q2SstVvq.qBTH0Y8AT6nDegH5z8RNdbQV/v8kPOB1H3d5F9zKj4XW","userCode":"61b717767173","id":1,"createTime":null,"updateTime":null}
I've minimized the processing and didn't actually do the autowrite
I think the error occurs because you have configured the wrong data type for the createTime and updateTime fields. The datetime data type does not have the CURRENT_TIMESTAMP value so the system can generate the data itself. You can change the following:
@CreateDateColumn({ name: 'create_time' })
createTime: Date
@UpdateDateColumn({ name: 'update_time' })
updateTime: Date