Invalid default value for ‘created_at’ (timestamp error)

문제상황

import { CreateDateColumn, UpdateDateColumn } from "typeorm";
export class SharedProp {
  @CreateDateColumn({
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP(6)',
    name: 'created_at'
  })
  createdAt: Date;
  @UpdateDateColumn({
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP(6)',
    nullable: true,
    name: 'updated_at'
  })
  updatedAt: Date;
}

위 코드에서 schema:sync 했는데 다음과 같은 에러가 나왔다.

Invalid default value for 'created_at'",
  query: "CREATE TABLE `users` (`created_at` timestamp(6) NOT NULL DEFAULT 'CURRENT_TIMESTAMP', `updated_at` timestamp(6) NULL DEFAULT 'CURRENT_TIMESTAMP' ON UPDATE CURRENT_TIMESTAMP(6), `id` int NOT NULL AUTO_INCREMENT, `first_name` varchar(255) NOT NULL, `last_name` varchar(255) NOT NULL, `age` int NOT NULL, `email` varchar(255) NOT NULL, UNIQUE INDEX `IDX_97672ac88f789774dd47f7c8be` (`email`), PRIMARY KEY (`id`)) ENGINE=InnoDB"

조사

https://stackoverflow.com/questions/45386614/mysql-invalid-default-value-for-timestamp

CURRENT_TIMESTAMP가 아니라, CURRENT_TIMESTAMP(6)을 써야한단다.

해결책 정리

export class SharedProp {
  @CreateDateColumn({
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP(6)',
    name: 'created_at'
  })
  createdAt: Date;
  @UpdateDateColumn({
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP(6)(6),
    nullable: true,
    name: 'updated_at'
  })
  updatedAt: Date;
}

굳! 잘된다 😀

Leave a Reply

Your email address will not be published.