Skip to content

Migration keeps changing @CreateDateColumn/@UpdateDateColumn timestamp column to same definition #3991

@cowpewter

Description

@cowpewter

Issue type:

[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[x] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[ ] latest
[ ] @next
[x] 0.2.16 (or put your version here)

Steps to reproduce or a small repository showing the problem:

I have a bunch of tables with my create- and updateTimestamp columns defined as follows:

  @CreateDateColumn({ type: 'timestamp', precision: null, default: () => 'CURRENT_TIMESTAMP' })
  createTimestamp: Date;

  @UpdateDateColumn({ type: 'timestamp', precision: null, default: () => 'CURRENT_TIMESTAMP' })
  updateTimestamp: Date;

When I run migration:generate, typeorm is repeatedly (even after running it) generating a migration file that changes the column definition to the same definition it already has. You can tell because the up and down methods do the exact same thing. I would expect the migration to be empty.

Example migration file

import {MigrationInterface, QueryRunner} from "typeorm";

export class TestTimestamps1555253092006 implements MigrationInterface {

    public async up(queryRunner: QueryRunner): Promise<any> {
        await queryRunner.query("ALTER TABLE `album` CHANGE `createTimestamp` `createTimestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP");
        await queryRunner.query("ALTER TABLE `album` CHANGE `updateTimestamp` `updateTimestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP");
    }

    public async down(queryRunner: QueryRunner): Promise<any> {
        await queryRunner.query("ALTER TABLE `album` CHANGE `updateTimestamp` `updateTimestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP");
        await queryRunner.query("ALTER TABLE `album` CHANGE `createTimestamp` `createTimestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP");
    }

}

I can work around it by defining my create- and updateTimestamp columns as follows

  @Column('timestamp', {
    default: () => 'CURRENT_TIMESTAMP',
  })
  createTimestamp: Date;

  @Column('timestamp', {
    default: () => 'CURRENT_TIMESTAMP',
    onUpdate: 'CURRENT_TIMESTAMP',
  })
  updateTimestamp: Date;

But it's a shame that it's not working with the @CreateDateColumn and @UpdateDateColumn decorators.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions