有两个表[弓详细信息和项目详细信息]。我实际上已经完成了两个表之间的关联
var BomDetail = sequelize.define('BomDetail', {
bomId: {
type: DataTypes.STRING,
field: 'bom_id',
unique: true
},
createdBy: {
type: DataTypes.STRING,
field: 'created_by'
},
BomDetail.hasMany(models.ItemDetail, {
foreignKey: 'bomId',
sourceKey: 'bomId',
onDelete: 'cascade'
});
var ItemDetail = sequelize.define('ItemDetail', {
itemId: {
type: DataTypes.STRING,
field: 'item_id',
unique: true
},
holdDate: {
type: DataTypes.DATE,
field: 'hold_date',
},
bomId: {
type: DataTypes.STRING,
field: 'bom_id'
},
ItemDetail.associate = function(models) {
ItemDetail.belongsTo(models.BomDetail, {
foreignKey: 'bomId',
targetKey: 'bomId',
onDelete: 'cascade'
});
};
当我做
Models.ItemDetail.findAll({
include: {
model: Models.BomDetail
}
})
正常结果是在项目明细里面是bomdetail
{
"id": 3,
"itemId": "2397saf8enaeflk8",
"productId": "afsggfg14fhg43t",
"productName": "Siemens meter",
"quantity": 3,
"followUpDate": "2018-05-15T18:30:00.000Z",
"leadTime": 10,
"releaseDate": "2018-05-20T18:30:00.000Z",
"holdDate": "2018-05-31T18:30:00.000Z",
"bomId": "azcpspp02_07141700_07_00_M00",
"status": "hold",
"created_at": "2018-05-14T09:20:15.658Z",
"updated_at": "2018-05-14T09:20:15.658Z",
"BomDetail": {
"id": 50,
"bomId": "azcpspp02_07141700_07_00_M00",
"createdBy": "azcpspp02",
"createdByName": "Siemens BLR -nry85322",
"bomCreatedDate": "2018-02-26T00:00:00.000Z",
"soldToAcc": "30000006",
"status": "hold",
"created_at": "2018-05-11T11:58:46.267Z",
"updated_at": "2018-05-11T11:58:46.267Z"
}
},
但是当我这样做
Models.BomDetail.findAll({
include: {
model: Models.ItemDetail:
}
})
错误会来..
将nvarchar值'azcpspp02_07141700_07_00_M00'转换为数据类型int时,转换失败。
它比较由BomDetail表中的sequelize生成的主键和ItemDetail的外键
喜欢
...左外部联接[lta_item_detail] AS [ItemDetails]启用[BomDetail]。[id] = [ItemDetails]。[bom_id];
但是我想比较BomDetail表的唯一键(bomId)和ItemDetail表的相应外键(BomId)。
(i.e)...左外部联接[lta_item_detail] AS [ItemDetails] ON [BomDetail]。[bom_id] = [ItemDetails]。[bom_id];
我该怎么办?
最佳答案
尝试从ItemDetail模型中删除bomId,并应用false来应用左外部联接。
Models.BomDetail.findAll({
include: {
model: Models.ItemDetail,
required : false
}
});