解决序列化连接中的异常错误



尝试在sequelize中运行连接时出现异常错误。错误信息是

SequelizeEagerLoadingError: tblSMS使用别名与tblSMSSent关联。您包含了一个别名(tblSMS),但它与关联(tblSM)中定义的别名不匹配。

我创建的join是:

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
})

这两个表的定义是使用sequize -auto创建的,这两个表之间没有对应关系。

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'
});

最新更新