Skip to content

Inappropriate migration generated #2943

@jonaaix

Description

@jonaaix

Issue type:

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

Database system/driver:

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

TypeORM version:

[ x ] latest 0.2.7
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

When using an unsigned PrimaryGenerateColumn, and generating a migration using typeorm migration:generate, the generated migrations get into a loop. The generated migration is always the same, because the query leads to an undocumented change in the database, which causes migration:generate to detect the change again and again.

Reproduction:

import { Entity, PrimaryGeneratedColumn, Column, PrimaryColumn, Generated } from 'typeorm';

/**
 * Log action
 */
@Entity('log_action')
export class LogAction {
   /**
    * logActionId
    */
   @PrimaryGeneratedColumn({
      name: 'log_action_id',
      unsigned: true
   })
   public logActionId: number;

   /**
    * title
    */
   @Column('varchar', {
      nullable: false,
      length: 150,
      name: 'title'
   })
   public title: string;
   /**
    * created
    */
   @Column('datetime', {
      nullable: false,
      default: () => 'CURRENT_TIMESTAMP',
      name: 'created'
   })
   public created: Date;

   /**
    * last_modified
    */
   @Column('datetime', {
      nullable: false,
      default: () => 'CURRENT_TIMESTAMP',
      onUpdate: 'CURRENT_TIMESTAMP',
      name: 'last_modified'
   })
   public lastModified: Date;
}
  • Create table
    ts-node ./node_modules/.bin/typeorm schema:sync
  • Create migration
    ts-node ./node_modules/.bin/typeorm migration:generate -n test

You can run migration:generate and migration:run multiple times, there will always be this migration generated, again and again.

import {MigrationInterface, QueryRunner} from "typeorm";

export class test1539781487192 implements MigrationInterface {

    public async up(queryRunner: QueryRunner): Promise<any> {
        await queryRunner.query("ALTER TABLE `log_action` CHANGE `log_action_id` `log_action_id` int UNSIGNED NOT NULL AUTO_INCREMENT");
    }

    public async down(queryRunner: QueryRunner): Promise<any> {
        await queryRunner.query("ALTER TABLE `log_action` CHANGE `log_action_id` `log_action_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT");
    }

}

At this point, it seems to be an issue of not defining a precision, but even adding precision: 10 to PrimaryGeneratedColumn config doesn't solve it. It produces the following migration:

import { MigrationInterface, QueryRunner } from 'typeorm';

export class test1539782429566 implements MigrationInterface {

   public async up(queryRunner: QueryRunner): Promise<any> {
      await queryRunner.query('ALTER TABLE `log_action` CHANGE `log_action_id` `log_action_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT');
   }

   public async down(queryRunner: QueryRunner): Promise<any> {
      await queryRunner.query('ALTER TABLE `log_action` CHANGE `log_action_id` `log_action_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT');
   }

}

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