belongsToMany和targetKey选项是否存在任何已知问题?
我的模型定义如下:
'use strict';
module.exports = (sequelize, DataTypes) => {
const Keyword = sequelize.define('Keyword', {
name: DataTypes.STRING
}, {});
Keyword.associate = function(models) {
Keyword.belongsToMany(models.Location, {
through: models.SearchRequest
})
Keyword.belongsToMany( models.Ranking, {
targetKey: 'searchRequestId',
otherKey: {
name: 'id'
},
through: {
model: models.SearchRequest,
}
})
}
return Keyword;
};
运行以下命令后:
kwrr > keyword = await Keyword.findOne()()
kwrr > await keyword.getRankings()
生成的sql如下:
Executing (default):
SELECT
`Ranking`.`id`, `Ranking`.`googlePageRank`, `Ranking`.`googleResultRank`,
`Ranking`.`searchRequestId`, `Ranking`.`createdAt`, `Ranking`.`updatedAt`,
`SearchRequest`.`locationId` AS `SearchRequest.locationId`, `SearchRequest`.`keywordId` AS
`SearchRequest.keywordId`, `SearchRequest`.`websiteId` AS `SearchRequest.websiteId`,
`SearchRequest`.`createdAt` AS `SearchRequest.createdAt`, `SearchRequest`.`updatedAt` AS
`SearchRequest.updatedAt`, `SearchRequest`.`KeywordId` AS `SearchRequest.KeywordId`,
`SearchRequest`.`LocationId` AS `SearchRequest.LocationId`
FROM `Rankings` AS `Ranking`
INNER JOIN `SearchRequests` AS `SearchRequest`
ON `Ranking`.`id` = `SearchRequest`.`id` AND
`SearchRequest`.`KeywordId` = 101;
我期望的是Rank.id,而不是Rank.id。 INNER JOIN SearchRequests AS SearchRequest ON Ranking.searchRequestId = SearchRequest.id
。
根据to the source,它的targetKey应该启用它。
知道为什么它没有生效吗?
p.s我一直遵循源代码,并且targetKey的值已按预期传递,但是生成的sql似乎不兑现它。
谢谢!