Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inappropiate migration generated for 'default: null' #5509

Closed
thefat32 opened this issue Feb 12, 2020 · 1 comment · Fixed by #5517
Closed

Inappropiate migration generated for 'default: null' #5509

thefat32 opened this issue Feb 12, 2020 · 1 comment · Fixed by #5517

Comments

@thefat32
Copy link
Contributor

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:

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

Steps to reproduce or a small repository showing the problem:

When using the option default: null for an entity's column, and generating a migration using typeorm migration:generate, the generated migrations get into a loop. The generated migration is always the same, because 'null' default value is being normalized as null instead of NULL. When typeorm checks default value for the column it finds 'NULL' !== 'null'.

This bug is related to #3345

In order to reproduce with a simple User entity:

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  Unique,
  CreateDateColumn,
  UpdateDateColumn
} from 'typeorm';
import { Length } from 'class-validator';

@Entity()
@Unique(['username'])
export default class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  @Length(4, 20)
  username: string;

  @Column()
  @Length(4, 100)
  password: string;

  @Column({ nullable: true, default: null })
  country: string;

  @Column('simple-array')
  @IsNotEmpty()
  roles: string[];

  @Column()
  @CreateDateColumn()
  createdAt!: Date;

  @Column()
  @UpdateDateColumn()
  updatedAt: Date;
}
  • Sync Schema
ts-node ./node_modules/typeorm/cli.js schema:sync
  • Generate Migration
ts-node ./node_modules/typeorm/cli.js migration:generate -n DefaultNullTest

Obtained:

  • Up
ALTER TABLE `user` CHANGE `country` `country` varchar(255) NULL DEFAULT null
  • Down
ALTER TABLE `user` CHANGE `country` `country` varchar(255) NULL DEFAULT NULL

I think modifying MysqlDriver.ts line 593 to:

return `NULL`;

should fix the problem

@thefat32
Copy link
Contributor Author

Some change araised this bug again! Code line

    /**
     * Normalizes "default" value of the column.
     */
    normalizeDefault(columnMetadata: ColumnMetadata): string | undefined {
        const defaultValue = columnMetadata.default;

        if (defaultValue === null) {
            return undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant