sequelize: Cannot read property 'type' of undefined
Hi guys… Since I’ve updated Sequelize from v3.X to the last version (v4.x), I had a new issue… I got the error described in the title when i am doing my query. I’d debugged to see where can be the issue and I think that it is not from my side (I think…of course)…
This is the models definition I have:
group_user.ts
import * as sequelize from 'sequelize';
module.exports = (sequelize: sequelize.Sequelize, DataTypes: sequelize.DataTypes) => {
return sequelize.define('group_user', {
description: {
allowNull: true,
type: DataTypes.TEXT,
},
id: {
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
name: {
allowNull: true,
type: DataTypes.TEXT,
},
}, {
freezeTableName: true,
tableName: 'group_user',
underscored: true,
});
};
privilege.ts
import * as sequelize from 'sequelize';
module.exports = (sequelize: sequelize.Sequelize, DataTypes: sequelize.DataTypes) => {
return sequelize.define('privilege', {
code: {
allowNull: false,
defaultValue: 'N/D',
type: DataTypes.TEXT,
},
description: {
allowNull: true,
type: DataTypes.TEXT,
},
id: {
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
}, {
freezeTableName: true,
tableName: 'privilege',
underscored: true,
});
};
group_privilege.ts
import * as sequelize from 'sequelize';
module.exports = (sequelize: sequelize.Sequelize, DataTypes: sequelize.DataTypes) => {
return sequelize.define('group_privilege', {
group_id: {
allowNull: false,
references: {
key: 'id',
model: 'group_user',
},
type: DataTypes.INTEGER,
},
id: {
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
privilege_id: {
allowNull: false,
references: {
key: 'id',
model: 'privilege',
},
type: DataTypes.INTEGER,
},
}, {
freezeTableName: true,
tableName: 'group_privilege',
underscored: true,
});
};
And this is what I’m trying to do in my function:
const groupIdList = [1, 2, 3, 4, 5, 6, 7, 8, 9,10];
const privilege: sequelize.Model <any, any> = Mysequelize.import(path.join(process.cwd(), 'build/models/privilege.js'));
const groupPrivilege: sequelize.Model <any, any> = Mysequelize.import(path.join(process.cwd(),
'build/models/group_privilege.js'));
groupPrivilege.belongsTo(privilege, {foreignKey: 'privilege_id', targetKey: 'id'});
groupPrivilege.findAll({
attributes: ['group_id'],
include: [{attributes: ['id', 'description'], model: privilege}],
order: ['group_privilege.group_id', 'privilege.id'],
where: {group_id: groupIdList}})
.then((groupPrivileges: IDBGroupPrivileges[]): void => {
...
}).catch((error: any): void => {
next(`Error while finding privileges for user's group in database: ${error.message}`);
return null;
});
I think they are maybe an issue in the order process in Sequelize.
While debugging the issue, I ended up on the line 805 of the file \node_modules\sequelize\lib\dialects\abstact\query-generator.js
if (previousModel.rawAttributes[itemSplit[0]].type instanceof DataTypes.JSON) {
The following instruction:
previousModel.rawAttributes[itemSplit[0]]
returns undefined and of course, undefined.type
fires an exception (or error).
Can you help me please with this issue ? When using the v3.x of Sequelize, this code worked. I know that the v4 version of sequelize brings some breaking changes but with all I’ve seen about, I don’t think the issue comes from my side. Can you confirm me please my thoughts ?
Also, I’ve tried to replace the order key in the findAll function with order: [[groupPrivilege, 'group_id'], 'privilege.id'],
, with the same results
Thank you for you answers and your help.
Dialect: postgres Database version: 9.4.4 Sequelize version: 4.2.1
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 18 (1 by maintainers)
I fixed it using this guide!
I faced same issue. My issue was solved when i used Sequelize.literal(‘booth.boothname’): let data = await BoothKaryakarta.findAll({ include: [{ model: Booth, as: ‘booth’, required: true }], where:{ user_id: ctx.user.id },raw: false,order: [[Sequelize.literal(‘booth.boothname’), ‘ASC’]]});
In my case, the type I chose was type: Sequelize.VARCHAR instead of Sequelize.STRING and I got this “Cannot read property ‘type’ of undefined” instead of a meaningful error message. Traced back to this line https://github.com/sequelize/sequelize/blob/v4.38.0/lib/query-interface.js#L202
I am not finding it in master so it may be fixed.
Solved my issue by doing this:
order: [ 'group_id', [privilege, 'id']],
@sequelizeTeam: Is it the right way to use order key value ?
Thank you for your answer
This worked for me, but I had to add backticks! without I was getting
For who is still facing this issue, I solved the problem using the col method
For those who wander, this worked for me
I had to provide
as
that matches my reference frominclude
, otherwise it throws errorUnable to find a valid association for model
Solved my issue. The issue was with the ordering stuff. In 3.x you could use
.
between two relations, but with 4.x I had to switch to->
syntax.@TitaneBoy You could try:
In my case I previously (3.x) had:
But switching by so fixed the issue i was having: