question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
sandokanelcojocommented, Jul 5, 2019

EDIT: Probably a duplicate of #2737.

Same problem, happens also with foreign keys.

await queryRunner.query("ALTER TABLE `session` DROP FOREIGN KEY `FK_8e05f295cd772ec97ef56642192`");
await queryRunner.query("ALTER TABLE `session` ADD CONSTRAINT `FK_8e05f295cd772ec97ef56642192` FOREIGN KEY (`user`) REFERENCES `user`(`id`) ON DELETE NO ACTION ON UPDATE NO ACTION");

2reactions
guileencommented, May 17, 2019

same issue. also index .

export class patch21558112496598 implements MigrationInterface {

    public async up(queryRunner: QueryRunner): Promise<any> {
        await queryRunner.query("DROP INDEX `post_refPostId_likeCount_index` ON `post`");
        await queryRunner.query("DROP INDEX `post_createAt_index` ON `post`");
        await queryRunner.query("ALTER TABLE `post` DROP COLUMN `createAt`");
        await queryRunner.query("ALTER TABLE `post` ADD `createAt` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)");
        await queryRunner.query("ALTER TABLE `post` DROP COLUMN `updateAt`");
        await queryRunner.query("ALTER TABLE `post` ADD `updateAt` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)");
        await queryRunner.query("CREATE INDEX `post_refPostId_likeCount_index` ON `post` (`refPostId`, `likeCount`)");
        await queryRunner.query("CREATE INDEX `post_createAt_index` ON `post` (`createAt`)");
    }

    public async down(queryRunner: QueryRunner): Promise<any> {
        await queryRunner.query("DROP INDEX `post_createAt_index` ON `post`");
        await queryRunner.query("DROP INDEX `post_refPostId_likeCount_index` ON `post`");
        await queryRunner.query("ALTER TABLE `post` DROP COLUMN `updateAt`");
        await queryRunner.query("ALTER TABLE `post` ADD `updateAt` datetime(6) NOT NULL DEFAULT 'CURRENT_TIMESTAMP(6)'");
        await queryRunner.query("ALTER TABLE `post` DROP COLUMN `createAt`");
        await queryRunner.query("ALTER TABLE `post` ADD `createAt` datetime(6) NOT NULL DEFAULT 'CURRENT_TIMESTAMP(6)'");
        await queryRunner.query("CREATE INDEX `post_createAt_index` ON `post` (`createAt`)");
        await queryRunner.query("CREATE INDEX `post_refPostId_likeCount_index` ON `post` (`refPostId`, `likeCount`)");
    }

}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js add created_at and updated_at in entity of typeorm
Let's import first import { CreateDateColumn,UpdateDateColumn } from "typeorm";. Add this fields and decarators in Entity
Read more >
Remodeling the House While Living in It - Bossy Lobster
This migration can be coupled with a code change that starts to use the newly introduced columns in the view: created_at , updated_at...
Read more >
Decorator reference - typeorm - GitBook
By default, the column name is generated from the name of the property. You can change it by specifying your own name.
Read more >
11.2.6 Automatic Initialization and Updating for TIMESTAMP ...
With both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP , the column has the current timestamp for its default value and is automatically updated...
Read more >
TypeORM: Adding created_at and updated_at columns
In TypeORM, you can add a created_at column and a updated_at column by making use of the CreateDateColumn and UpdateDateColumn decorators, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found