我想更新hasMany关系的行,我有一个表InRoutes,该表可以包含许多InRouteDetails,所以当我更新InRoute时,我还需要更新它的详细信息,可能会发生三种情况,删除一个详细信息,更新细节并添加细节。
当我尝试使用setInRouteDetails进行更新时,由于某种原因,他尝试获取与给定该对象的对象相等的InRouteDetail,该错误是:截断了错误的DOUBLE值:“ [object Object]”
这是我的模特
export default (sequelize, DataType) => {
const InRoutes = sequelize.define('InRoutes', {
id: {
type: DataType.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataType.STRING,
allowNull: false,
validate: {
notEmpty: true,
},
},
description: {
type: DataType.TEXT,
allowNull: true,
},
createdBy: {
type: DataType.STRING,
allowNull: true,
},
updatedBy: {
type: DataType.STRING,
allowNull: true,
},
}, {
classMethods: {
associate: (models) => {
InRoutes.hasMany(models.InRoutesDetails, {
foreignKey: 'InRouteId',
as: 'InRouteDetails',
onDelete: 'CASCADE',
});
InRoutes.belongsTo(models.Trunks, {
foreignKey: 'trunkId',
as: 'TrunkId',
onDelete: 'CASCADE',
});
},
},
});
return InRoutes;
};
这是我尝试用其关联“ InRouteDetails”更新“ InRoute”的地方:
async update(data, params, updatedBy) {
data.updatedBy = updatedBy.username;
try {
const inRoute = await this.InRoutes.findOne({ where: params });
const teste = [{
id: 2,
mask: '89',
remove: 4,
add: '3',
destinationType: 'callback',
destination: 2,
InRouteId: 2,
}];
await inRoute.addInRouteDetails(teste);
await inRoute.update(data);
return defaultResponse(inRoute);
} catch (error) {
return errorResponse(error.message, HttpStatus.UNPROCESSABLE_ENTITY);
}
}
这在控制台上给了我这个结果:
Executing (default): UPDATE `InRoutesDetails` SET `InRouteId`=?,`updatedAt`=? WHERE `id` IN ('[object Object]')
他试图将id与某些对象进行比较。