Repository navigation
Description
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
Kononnable commentedon Apr 2, 2019
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?
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.
emamulandalib commentedon Apr 7, 2019
@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.
emamulandalib commentedon Apr 17, 2019
@Kononnable, how can I share data with you? I think TYPEORM should check if indices array has length. It will be wise.
Kononnable commentedon Apr 19, 2019
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.
emamulandalib commentedon Apr 20, 2019
I want to send you an email where I will provide you the detail about it.
Kononnable commentedon Apr 20, 2019
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).
dgreene1 commentedon Jul 24, 2019
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:
TypeError: Cannot read property 'id' of undefinedimnotjames commentedon Oct 11, 2020
Is it possible to create a reproducible example that exhibits this issue? Perhaps in a test format.
imnotjames commentedon Oct 16, 2020
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.
imnotjames commentedon Oct 16, 2020
Duplicate of #1772