Skip to content

Cannot read property 'name' of undefined (During migration file generation) #3835

Closed

Description

@emamulandalib

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.15 (or put your version here)

Problem

I have got below error when I have run: yarn typeorm migration:generate -n User

Error during migration generation:
TypeError: Cannot read property 'name' of undefined
    at MysqlQueryRunner.<anonymous> (/mnt/Other/Projects/node-api/src/driver/mysql/MysqlQueryRunner.ts:689:14)
    at step (/mnt/Other/Projects/node-api/node_modules/tslib/tslib.js:133:27)
    at Object.next (/mnt/Other/Projects/node-api/node_modules/tslib/tslib.js:114:57)
    at /mnt/Other/Projects/node-api/node_modules/tslib/tslib.js:107:75
    at new Promise (<anonymous>)
    at Object.__awaiter (/mnt/Other/Projects/node-api/node_modules/tslib/tslib.js:103:16)
    at MysqlQueryRunner.changeColumn (/mnt/Other/Projects/node-api/node_modules/typeorm/driver/mysql/MysqlQueryRunner.js:652:24)
    at /mnt/Other/Projects/node-api/node_modules/typeorm/driver/mysql/MysqlQueryRunner.js:840:141
    at /mnt/Other/Projects/node-api/src/util/PromiseUtils.ts:34:24
    at process._tickCallback (internal/process/next_tick.js:68:7)
error Command failed with exit code 1.

History

I am working under a team. Previously we were building projects using a framework. I am not mentioning it here for some reasons. Right now we are migrating the models for various usage using TypeORM. During migration I have found the upper issue. I have debugged the issue and found that the PrimaryColumn was labeled as UNIQUE in the indices. TypeORM somehow ignored it for some reason. I am trying to understand. During migration I have printed below object over which TypeORM is generating the migration files where I have found that sometimes indices array can be empty.

Table {
  columns:
   [ TableColumn {
       isNullable: false,
       isGenerated: true,
       isPrimary: true,
       isUnique: true,
       isArray: false,
       length: '',
       zerofill: false,
       unsigned: false,
       name: 'id',
       type: 'int',
       width: undefined,
       charset: undefined,
       collation: undefined,
       precision: undefined,
       scale: undefined,
       default: undefined,
       onUpdate: undefined,
       generationStrategy: 'increment',
       comment: '',
       enum: undefined,
       asExpression: undefined,
       generatedType: undefined,
       spatialFeatureType: undefined,
       srid: undefined },
     TableColumn {
       isNullable: true,
       isGenerated: false,
       isPrimary: false,
       isUnique: false,
       isArray: false,
       length: '',
       zerofill: false,
       unsigned: false,
       name: 'createdAt',
       type: 'datetime',
       width: undefined,
       charset: undefined,
       collation: undefined,
       precision: 0,
       scale: undefined,
       default: undefined,
       onUpdate: undefined,
       generationStrategy: undefined,
       comment: '',
       enum: undefined,
       asExpression: undefined,
       generatedType: undefined,
       spatialFeatureType: undefined,
       srid: undefined },
     TableColumn {
       isNullable: true,
       isGenerated: false,
       isPrimary: false,
       isUnique: false,
       isArray: false,
       length: '',
       zerofill: false,
       unsigned: false,
       name: 'updatedAt',
       type: 'datetime',
       width: undefined,
       charset: undefined,
       collation: undefined,
       precision: 0,
       scale: undefined,
       default: undefined,
       onUpdate: undefined,
       generationStrategy: undefined,
       comment: '',
       enum: undefined,
       asExpression: undefined,
       generatedType: undefined,
       spatialFeatureType: undefined,
       srid: undefined },
     TableColumn {
       isNullable: true,
       isGenerated: false,
       isPrimary: false,
       isUnique: false,
       isArray: false,
       length: '255',
       zerofill: false,
       unsigned: false,
       name: 'username',
       type: 'varchar',
       width: undefined,
       charset: undefined,
       collation: undefined,
       precision: undefined,
       scale: undefined,
       default: undefined,
       onUpdate: undefined,
       generationStrategy: undefined,
       comment: '',
       enum: undefined,
       asExpression: undefined,
       generatedType: undefined,
       spatialFeatureType: undefined,
       srid: undefined },
     TableColumn {
       isNullable: true,
       isGenerated: false,
       isPrimary: false,
       isUnique: false,
       isArray: false,
       length: '255',
       zerofill: false,
       unsigned: false,
       name: 'email',
       type: 'varchar',
       width: undefined,
       charset: undefined,
       collation: undefined,
       precision: undefined,
       scale: undefined,
       default: undefined,
       onUpdate: undefined,
       generationStrategy: undefined,
       comment: '',
       enum: undefined,
       asExpression: undefined,
       generatedType: undefined,
       spatialFeatureType: undefined,
       srid: undefined },
     TableColumn {
       isNullable: true,
       isGenerated: false,
       isPrimary: false,
       isUnique: false,
       isArray: false,
       length: '255',
       zerofill: false,
       unsigned: false,
       name: 'password',
       type: 'varchar',
       width: undefined,
       charset: undefined,
       collation: undefined,
       precision: undefined,
       scale: undefined,
       default: undefined,
       onUpdate: undefined,
       generationStrategy: undefined,
       comment: '',
       enum: undefined,
       asExpression: undefined,
       generatedType: undefined,
       spatialFeatureType: undefined,
       srid: undefined },
     TableColumn {
       isNullable: true,
       isGenerated: false,
       isPrimary: false,
       isUnique: false,
       isArray: false,
       length: '32',
       zerofill: false,
       unsigned: false,
       name: 'mobileNumber',
       type: 'varchar',
       width: undefined,
       charset: undefined,
       collation: undefined,
       precision: undefined,
       scale: undefined,
       default: undefined,
       onUpdate: undefined,
       generationStrategy: undefined,
       comment: '',
       enum: undefined,
       asExpression: undefined,
       generatedType: undefined,
       spatialFeatureType: undefined,
       srid: undefined },
     TableColumn {
       isNullable: true,
       isGenerated: false,
       isPrimary: false,
       isUnique: false,
       isArray: false,
       length: '255',
       zerofill: false,
       unsigned: false,
       name: 'uuid',
       type: 'varchar',
       width: undefined,
       charset: undefined,
       collation: undefined,
       precision: undefined,
       scale: undefined,
       default: undefined,
       onUpdate: undefined,
       generationStrategy: undefined,
       comment: '',
       enum: undefined,
       asExpression: undefined,
       generatedType: undefined,
       spatialFeatureType: undefined,
       srid: undefined },
     TableColumn {
       isNullable: true,
       isGenerated: false,
       isPrimary: false,
       isUnique: false,
       isArray: false,
       length: '',
       zerofill: false,
       unsigned: false,
       name: 'signupSource',
       type: 'double',
       width: undefined,
       charset: undefined,
       collation: undefined,
       precision: 22,
       scale: undefined,
       default: undefined,
       onUpdate: undefined,
       generationStrategy: undefined,
       comment: '',
       enum: undefined,
       asExpression: undefined,
       generatedType: undefined,
       spatialFeatureType: undefined,
       srid: undefined },
     TableColumn {
       isNullable: true,
       isGenerated: false,
       isPrimary: false,
       isUnique: false,
       isArray: false,
       length: '',
       zerofill: false,
       unsigned: false,
       name: 'facebookId',
       type: 'double',
       width: undefined,
       charset: undefined,
       collation: undefined,
       precision: 22,
       scale: undefined,
       default: undefined,
       onUpdate: undefined,
       generationStrategy: undefined,
       comment: '',
       enum: undefined,
       asExpression: undefined,
       generatedType: undefined,
       spatialFeatureType: undefined,
       srid: undefined },
     TableColumn {
       isNullable: true,
       isGenerated: false,
       isPrimary: false,
       isUnique: false,
       isArray: false,
       length: '',
       zerofill: false,
       unsigned: false,
       name: 'googleId',
       type: 'double',
       width: undefined,
       charset: undefined,
       collation: undefined,
       precision: 22,
       scale: undefined,
       default: undefined,
       onUpdate: undefined,
       generationStrategy: undefined,
       comment: '',
       enum: undefined,
       asExpression: undefined,
       generatedType: undefined,
       spatialFeatureType: undefined,
       srid: undefined },
     TableColumn {
       isNullable: true,
       isGenerated: false,
       isPrimary: false,
       isUnique: false,
       isArray: false,
       length: '',
       zerofill: false,
       unsigned: false,
       name: 'flag',
       type: 'tinyint',
       width: 1,
       charset: undefined,
       collation: undefined,
       precision: undefined,
       scale: undefined,
       default: undefined,
       onUpdate: undefined,
       generationStrategy: undefined,
       comment: '',
       enum: undefined,
       asExpression: undefined,
       generatedType: undefined,
       spatialFeatureType: undefined,
       srid: undefined },
     TableColumn {
       isNullable: false,
       isGenerated: false,
       isPrimary: false,
       isUnique: false,
       isArray: false,
       length: '',
       zerofill: false,
       unsigned: false,
       name: 'createdTime',
       type: 'timestamp',
       width: undefined,
       charset: undefined,
       collation: undefined,
       precision: 6,
       scale: undefined,
       default: 'CURRENT_TIMESTAMP(6)',
       onUpdate: undefined,
       generationStrategy: undefined,
       comment: '',
       enum: undefined,
       asExpression: undefined,
       generatedType: undefined,
       spatialFeatureType: undefined,
       srid: undefined },
     TableColumn {
       isNullable: false,
       isGenerated: false,
       isPrimary: false,
       isUnique: false,
       isArray: false,
       length: '',
       zerofill: false,
       unsigned: false,
       name: 'updatedTime',
       type: 'timestamp',
       width: undefined,
       charset: undefined,
       collation: undefined,
       precision: 6,
       scale: undefined,
       default: 'CURRENT_TIMESTAMP(6)',
       onUpdate: undefined,
       generationStrategy: undefined,
       comment: '',
       enum: undefined,
       asExpression: undefined,
       generatedType: undefined,
       spatialFeatureType: undefined,
       srid: undefined } ],
  indices: [],
  foreignKeys: [],
  uniques: [],
  checks: [],
  exclusions: [],
  justCreated: false,
  name: 'user',
  engine: undefined }

Model code

@Entity()
@Unique(['username', 'email', 'uuid', 'facebookId', 'googleId'])
export class User extends EntityDomain {
    @Column({
        length: 255,
        nullable: false
    })
    username: string

    @Column({
        length: 255,
        nullable: false
    })
    email: string

    @Column({
        length: 255,
        nullable: false
    })
    password: string

    @Column({
        length: 32,
        nullable: true
    })
    mobileNumber: string

    @Column({
        length: 255,
        nullable: false
    })
    uuid: string

    @Column({
        type: 'enum',
        enum: SignupSourceEnum,
        nullable: false
    })
    signupSource: number

    @Column({
        type: 'double',
        nullable: true
    })
    facebookId: number

    @Column({
        type: 'double',
        nullable: true
    })
    googleId: number

    @Column({
        type: 'tinyint',
        default: 1
    })
    flag: boolean
}

Temporary Fix

I have temporarily updated here like below: Link to the line!

...
if(clonedTable.indices.length) {
  uniqueIndex_1 = clonedTable.indices.find(function (index) {
    return index.columnNames.length === 1 && index.isUnique === true && !!index.columnNames.find(function (columnName) { return columnName === newColumn.name; });
});

  clonedTable.indices.splice(clonedTable.indices.indexOf(uniqueIndex_1), 1);
   tableUnique = clonedTable.uniques.find(function (unique) { return unique.name === uniqueIndex_1.name; });
  clonedTable.uniques.splice(clonedTable.uniques.indexOf(tableUnique), 1);
  upQueries.push("ALTER TABLE " + this.escapeTableName(table) + " DROP INDEX `" + uniqueIndex_1.name + "`");
  downQueries.push("ALTER TABLE " + this.escapeTableName(table) + " ADD UNIQUE INDEX `" + uniqueIndex_1.name + "` (`" + newColumn.name + "`)");
 }
...

I am looking for a permanent fix from TypeORM team. Thank you.

Activity

  1. Kononnable commented on Apr 2, 2019

    @Kononnable
    Contributor

    Can you provide some reproduction data? It would be hard to fix it and make a test so it won't broke in the future without a way to reproduce the issue.

    @Unique(['username', 'email', 'uuid', 'facebookId', 'googleId'])
    Just making sure - you want combination of every column to be unique or just each column on their own?

    I have debugged the issue and found that the PrimaryColumn was labeled as UNIQUE in the indices.TypeORM somehow ignored it for some reason. I am trying to understand.

    If I understood you correctly - PKs are always unique, so there is no reason to make a separate indices for it and explicitly say that they are unique.

  2. emamulandalib commented on Apr 7, 2019

    @emamulandalib
    Author

    @Kononnable you are absolutely right. Primary key should not have any additional indices. I have mentioned before that, we were using a framework which was producing this kind of behavior. I will provide some data for fixing. Please wait for my next reply. Thank you.

  3. emamulandalib commented on Apr 17, 2019

    @emamulandalib
    Author

    @Kononnable, how can I share data with you? I think TYPEORM should check if indices array has length. It will be wise.

  4. Kononnable commented on Apr 19, 2019

    @Kononnable
    Contributor

    You could create gist/repository repo/failing test and post it here. If data is confidential and you don't want it to be public then the best way is probably by email.
    It's a migration issue, so we need state of things when everything is working as expected and small description of the change which breaks the functionality.

  5. emamulandalib commented on Apr 20, 2019

    @emamulandalib
    Author

    I want to send you an email where I will provide you the detail about it.

  6. Kononnable commented on Apr 20, 2019

    @Kononnable
    Contributor

    Ok, Email address is avaliable on my github profile(https://github.com/Kononnable) However I don't have a clue when I will have time to look at it(and if I will be able to help) - that's why it's better to post reproduction data for everyone(ofc. if it's not confidential).

  7. dgreene1 commented on Jul 24, 2019

    @dgreene1

    FYI this is reproduce-able anytime that your entity has a constructor (Which I'd like to have since I enjoy tsconfig's strictPropertyInitialization).

    So this reproduces the issue:

    import {
        Entity,
        PrimaryColumn
    } from "typeorm";
    
    @Entity()
    export class EntityThatCantBeGenerated  {
    
        @PrimaryColumn("varchar")
        public readonly id: string;
    
        constructor(input : { id : string }){
            this.id = input.id;
        }
    }
    

    TypeError: Cannot read property 'id' of undefined

  8. imnotjames commented on Oct 11, 2020

    @imnotjames
    Contributor

    Is it possible to create a reproducible example that exhibits this issue? Perhaps in a test format.

  9. self-assigned this
    on Oct 15, 2020
  10. imnotjames commented on Oct 16, 2020

    @imnotjames
    Contributor

    This definitely looks like the constructor existing and not getting the properties it needs. As a workaround, set defaults on the constructor to handle that.

    I'll link this as a duplicate of the issue requesting us to skip constructors when loading from the database.

  11. imnotjames commented on Oct 16, 2020

    @imnotjames
    Contributor

    Duplicate of #1772

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions