解决sequelize join中的异常错误
Solve an unusual error in a sequelize join
我在尝试 运行 加入 sequelize 时遇到异常错误。错误信息是
SequelizeEagerLoadingError: tblSMS is associated to tblSMSSent using an alias. You've included an alias (tblSMS), but it does not match the alias(es) defined in your association (tblSM).
我创建的连接是这样的:
userDB.tblSMSSent.belongsTo(userDB.tblSMS, { foreignKey: 'MessageId', targetKey: 'OutgoingMessageId' });
userDB.tblSMS.hasMany(userDB.tblSMSSent, { targetKey: 'OutgoingMessageId' });
let messages = await userDB.tblSMSSent.findAll({
attributes: [
['phone', 'phone'],
['date', 'date'],
[sequelize.fn('substring', sequelize.col('result'), 1, 50), 'result'],
['debtor', 'debtor'],
['sms', 'OrigBody'],
['user', 'user'],
['MessageId', 'MessageId']
],
offset: startVal,
include: {
model: userDB.tblSMS,
as: 'tblSMS',
attributes: [['MessageBody', 'MessageText']]
},
logging: console.log
})
2 个表的定义是使用 sequelize-auto 创建的,2 个表之间没有对应关系。
tblSMS
const Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
return sequelize.define('tblSMS', {
id: {
autoIncrement: true,
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
Destination: {
type: DataTypes.STRING(20),
allowNull: true
},
MessageBody: {
type: DataTypes.TEXT,
allowNull: true
},
MessageNumber: {
type: DataTypes.INTEGER,
allowNull: true
},
OutgoingMessageId: {
type: DataTypes.INTEGER,
allowNull: true
},
PhoneNumber: {
type: DataTypes.CHAR(20),
allowNull: true
},
ReceivedDate: {
type: DataTypes.DATE,
allowNull: true
},
Reference: {
type: DataTypes.TEXT,
allowNull: true
},
exported: {
type: DataTypes.BOOLEAN,
allowNull: true,
defaultValue: false
},
Key: {
type: DataTypes.CHAR(32),
allowNull: true
},
Province: {
type: DataTypes.CHAR(2),
allowNull: true
},
OrigBody: {
type: DataTypes.TEXT,
allowNull: true
},
OrigNumber: {
type: DataTypes.CHAR(20),
allowNull: true
},
OrigCredits: {
type: DataTypes.INTEGER,
allowNull: true
},
OrigQueueDate: {
type: DataTypes.DATEONLY,
allowNull: true
},
OrigSendDate: {
type: DataTypes.DATEONLY,
allowNull: true
},
OrigSent: {
type: DataTypes.BOOLEAN,
allowNull: true
},
OrigSuccess: {
type: DataTypes.STRING(50),
allowNull: true
},
AccountKey: {
type: DataTypes.BLOB,
allowNull: true
}
}, {
sequelize,
tableName: 'tblSMS',
schema: 'dbo',
timestamps: false,
indexes: [
{
name: "PK_tblSMS",
unique: true,
fields: [
{ name: "id" },
]
},
]
});
};
tblSMSSent
const Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
return sequelize.define('tblSMSSent', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
user: {
type: DataTypes.CHAR(10),
allowNull: false
},
phone: {
type: DataTypes.CHAR(15),
allowNull: false
},
sms: {
type: DataTypes.TEXT,
allowNull: true
},
date: {
type: DataTypes.DATE,
allowNull: false
},
result: {
type: DataTypes.TEXT,
allowNull: true
},
debtor: {
type: DataTypes.CHAR(20),
allowNull: false
},
exported: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
},
messageID: {
type: DataTypes.CHAR(20),
allowNull: true
}
}, {
sequelize,
tableName: 'tblSMSSent',
schema: 'dbo',
timestamps: false
});
};
当我尝试通过扩展定义向 tblSMSSent 添加自定义 getter 方法时出现此错误(我无法开始工作 - 我想在从中检索到的文本之前添加一个“^” tblSMSSent).
此错误的不寻常之处在于,在代码中的任何地方都找不到任何带有简单“tblSM”的文本,因为错误消息报告为使用的别名。
我停止并重新启动了 Node 几次都无济于事。
错误是说你在findAll
查询的include
部分有一个别名,但是在两个表之间的关联定义中没有对应的别名。尝试只
include: {
model: userDB.tblSMS,
attributes: [['MessageBody', 'MessageText']]
},
在 findAll
查询中。这将删除对未定义别名的引用。
此外,行
userDB.tblSMS.hasMany(userDB.tblSMSSent, { targetKey: 'OutgoingMessageId' });
应该改为
userDB.tblSMS.hasMany(userDB.tblSMSSent, { sourceKey: 'OutgoingMessageId' });
因为 OutgoingMessageId
是源模型而不是目标模型的 属性。您甚至可以将 foreignKey
属性 添加到 hasMany
关联中,这样
userDB.tblSMS.hasMany(userDB.tblSMSSent, {
sourceKey: 'OutgoingMessageId',
foreignKey: 'MessageId'
});
我在尝试 运行 加入 sequelize 时遇到异常错误。错误信息是
SequelizeEagerLoadingError: tblSMS is associated to tblSMSSent using an alias. You've included an alias (tblSMS), but it does not match the alias(es) defined in your association (tblSM).
我创建的连接是这样的:
userDB.tblSMSSent.belongsTo(userDB.tblSMS, { foreignKey: 'MessageId', targetKey: 'OutgoingMessageId' });
userDB.tblSMS.hasMany(userDB.tblSMSSent, { targetKey: 'OutgoingMessageId' });
let messages = await userDB.tblSMSSent.findAll({
attributes: [
['phone', 'phone'],
['date', 'date'],
[sequelize.fn('substring', sequelize.col('result'), 1, 50), 'result'],
['debtor', 'debtor'],
['sms', 'OrigBody'],
['user', 'user'],
['MessageId', 'MessageId']
],
offset: startVal,
include: {
model: userDB.tblSMS,
as: 'tblSMS',
attributes: [['MessageBody', 'MessageText']]
},
logging: console.log
})
2 个表的定义是使用 sequelize-auto 创建的,2 个表之间没有对应关系。
tblSMS
const Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
return sequelize.define('tblSMS', {
id: {
autoIncrement: true,
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
Destination: {
type: DataTypes.STRING(20),
allowNull: true
},
MessageBody: {
type: DataTypes.TEXT,
allowNull: true
},
MessageNumber: {
type: DataTypes.INTEGER,
allowNull: true
},
OutgoingMessageId: {
type: DataTypes.INTEGER,
allowNull: true
},
PhoneNumber: {
type: DataTypes.CHAR(20),
allowNull: true
},
ReceivedDate: {
type: DataTypes.DATE,
allowNull: true
},
Reference: {
type: DataTypes.TEXT,
allowNull: true
},
exported: {
type: DataTypes.BOOLEAN,
allowNull: true,
defaultValue: false
},
Key: {
type: DataTypes.CHAR(32),
allowNull: true
},
Province: {
type: DataTypes.CHAR(2),
allowNull: true
},
OrigBody: {
type: DataTypes.TEXT,
allowNull: true
},
OrigNumber: {
type: DataTypes.CHAR(20),
allowNull: true
},
OrigCredits: {
type: DataTypes.INTEGER,
allowNull: true
},
OrigQueueDate: {
type: DataTypes.DATEONLY,
allowNull: true
},
OrigSendDate: {
type: DataTypes.DATEONLY,
allowNull: true
},
OrigSent: {
type: DataTypes.BOOLEAN,
allowNull: true
},
OrigSuccess: {
type: DataTypes.STRING(50),
allowNull: true
},
AccountKey: {
type: DataTypes.BLOB,
allowNull: true
}
}, {
sequelize,
tableName: 'tblSMS',
schema: 'dbo',
timestamps: false,
indexes: [
{
name: "PK_tblSMS",
unique: true,
fields: [
{ name: "id" },
]
},
]
});
};
tblSMSSent
const Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
return sequelize.define('tblSMSSent', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
user: {
type: DataTypes.CHAR(10),
allowNull: false
},
phone: {
type: DataTypes.CHAR(15),
allowNull: false
},
sms: {
type: DataTypes.TEXT,
allowNull: true
},
date: {
type: DataTypes.DATE,
allowNull: false
},
result: {
type: DataTypes.TEXT,
allowNull: true
},
debtor: {
type: DataTypes.CHAR(20),
allowNull: false
},
exported: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
},
messageID: {
type: DataTypes.CHAR(20),
allowNull: true
}
}, {
sequelize,
tableName: 'tblSMSSent',
schema: 'dbo',
timestamps: false
});
};
当我尝试通过扩展定义向 tblSMSSent 添加自定义 getter 方法时出现此错误(我无法开始工作 - 我想在从中检索到的文本之前添加一个“^” tblSMSSent).
此错误的不寻常之处在于,在代码中的任何地方都找不到任何带有简单“tblSM”的文本,因为错误消息报告为使用的别名。
我停止并重新启动了 Node 几次都无济于事。
错误是说你在findAll
查询的include
部分有一个别名,但是在两个表之间的关联定义中没有对应的别名。尝试只
include: {
model: userDB.tblSMS,
attributes: [['MessageBody', 'MessageText']]
},
在 findAll
查询中。这将删除对未定义别名的引用。
此外,行
userDB.tblSMS.hasMany(userDB.tblSMSSent, { targetKey: 'OutgoingMessageId' });
应该改为
userDB.tblSMS.hasMany(userDB.tblSMSSent, { sourceKey: 'OutgoingMessageId' });
因为 OutgoingMessageId
是源模型而不是目标模型的 属性。您甚至可以将 foreignKey
属性 添加到 hasMany
关联中,这样
userDB.tblSMS.hasMany(userDB.tblSMSSent, {
sourceKey: 'OutgoingMessageId',
foreignKey: 'MessageId'
});