Repository navigation
Closed
Description
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.
Reacted by Fanch
Metadata
Metadata
Assignees
Labels
No labels
Activity
fanch317 commentedon May 13, 2020
Indeed. The migration seem compare the string "'NULL'" to string "NULL" in this function :
typeorm/src/driver/mysql/MysqlDriver.ts
Line 912 in e92c743
qodunpob commentedon May 30, 2020
It seems that the correct solution in this problem is to remove the
default: null, keeping onlynullable: true.https://dev.mysql.com/doc/refman/5.7/en/data-type-defaults.html
sdankel commentedon Feb 18, 2021
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 }):when I try to run this migration it throws an error.