Skip to content

Inappropriate migration generated #6030

Closed

Description

@qodunpob

Issue type:

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

Database system/driver:

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

TypeORM version:

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

Steps to reproduce or a small repository showing the problem:

Hello, everyone. I have a problem with generated migration. In my model I have a property with null value by default:

@Entity()
export default class Category extends BaseEntity {
  /** Category`s id */
  @PrimaryGeneratedColumn('increment', { type: 'int', unsigned: true, width: 11 })
  id: number;

  /** Shop`s ID */
  @Column('int', { name: 'shop_id', width: 11, unsigned: true })
  @ManyToOne(() => Shop, shop => shop.id, {
    onUpdate: 'CASCADE',
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'shop_id' })
  shopId: number;

  /** Category`s name */
  @Column('varchar', { nullable: true, default: null, length: 45 }) // <-- this property
  name: string;

  /** Category`s products */
  @ManyToMany(() => Product, product => product.categories)
  products: Product[];
}

And this field has null value by default in DB.

mysql> SHOW COLUMNS FROM category;
+---------+------------------+------+-----+---------+----------------+
| Field   | Type             | Null | Key | Default | Extra          |
+---------+------------------+------+-----+---------+----------------+
| id      | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| shop_id | int(11) unsigned | NO   | MUL | NULL    |                |
| name    | varchar(45)      | YES  |     | NULL    |                |
+---------+------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

But every time, when I generate new migration, I get this:

public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query("ALTER TABLE `category` CHANGE `name` `name` varchar(45) NULL DEFAULT NULL", undefined);
}
public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query("ALTER TABLE `category` CHANGE `name` `name` varchar(45) NULL", undefined);
}

And with all other fields whose default value null happens the same.

Activity

  1. fanch317 commented on May 13, 2020

    @fanch317

    Indeed. The migration seem compare the string "'NULL'" to string "NULL" in this function :

    protected compareDefaultValues(columnMetadataValue: string, databaseValue: string): boolean {

  2. qodunpob commented on May 30, 2020

    @qodunpob
    Author

    It seems that the correct solution in this problem is to remove the default: null, keeping only nullable: true.

    https://dev.mysql.com/doc/refman/5.7/en/data-type-defaults.html

    If the column can take NULL as a value, the column is defined with an explicit DEFAULT NULL clause.

  3. sdankel commented on Feb 18, 2021

    @sdankel

    I have a similar problem as well. changing @Column({ default: null, nullable: true }) to @Column({ nullable: true }) had a worse effect -- these were generated in the migration file for @Column({ nullable: true }) :

            await queryRunner.query(`ALTER TABLE "table_name" ALTER COLUMN "column_name" SET NOT NULL`);
            await queryRunner.query(`COMMENT ON COLUMN "table_name"."column_name" IS NULL`);
            await queryRunner.query(`ALTER TABLE "table_name" ALTER COLUMN "column_name" SET DEFAULT null`);
    

    when I try to run this migration it throws an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions