我试图同时获得所有testset对象及其所有方案。
父模型
'use strict';
module.exports = (sequelize, DataTypes) => {
const Testset = sequelize.define('testset', {
name: DataTypes.STRING
}, {});
Testset.associate = function(models) {
};
return Testset;
};
模型孩子
'use strict';
module.exports = (sequelize, DataTypes) => {
const Scenario = sequelize.define('scenario', {
name: DataTypes.STRING,
mode: DataTypes.STRING,
route: DataTypes.STRING,
runQuantity: DataTypes.INTEGER,
isTextOnly: DataTypes.BOOLEAN,
testsetId: DataTypes.INTEGER
}, {});
Scenario.associate = function(models) {
};
return Scenario;
};
服务
const Promise = require('promise');
Scenario.belongsTo(Testset);
Testset.hasMany(Scenario);
readAllTestsets () {
return fromPromise(Testset.findAll({
raw: true,
include: [{
model: Scenario,
where: {
//testsetId: Testset.id
}
}]
}));
}
readTestsetById (testsetId: number) {
return fromPromise(Testset.findByPk(testsetId, {
include: [{
model: Scenario,
where: {
testsetId: testsetId
}
}]
}));
}
我希望在请求所有可用测试集的同时,将所有方案作为数组附加到引用的测试集(外键:testsetId)。它通过readTestsetById方法适用于单个测试集。我不明白为什么它会针对数据库中可用的场景数量复制testset对象,并将场景属性附加为case.attribute。
SELECT `testset`.`id`, `testset`.`name`, `testset`.`createdAt`, `testset`.`updatedAt`, `scenarios`.`id` AS `scenarios.id`, `scenarios`.`name` AS `scenarios.name`, `scenarios`.`mode` AS `scenarios.mode`, `scenarios`.`route` AS `scenarios.route`, `scenarios`.`runQuantity` AS `scenarios.runQuantity`, `scenarios`.`isTextOnly` AS `scenarios.isTextOnly`, `scenarios`.`testsetId` AS `scenarios.testsetId`, `scenarios`.`createdAt` AS `scenarios.createdAt`, `scenarios`.`updatedAt` AS `scenarios.updatedAt`
FROM `testsets` AS `testset`
INNER JOIN `scenarios` AS `scenarios` ON `testset`.`id` = `scenarios`.`testsetId`;
输出:
0:
id: 1
name: "Tobis Testset"
scenarios.createdAt: Thu Mar 21 2019 17:17:31 GMT+0100 (Central European Standard Time) {}
scenarios.id: 1
scenarios.isTextOnly: 0
scenarios.mode: "Adaptive Cruise Control (id, ACC)"
scenarios.name: "Tobisscenarios1"
scenarios.route: "Los Angeles"
scenarios.runQuantity: 5
scenarios.testsetId: 1
scenarios.updatedAt: Thu Mar 21 2019 17:17:31 GMT+0100 (Central European Standard Time) {}
updatedAt: Thu Mar 21 2019 17:17:31 GMT+0100 (Central European Standard Time) {}
__proto__: Object
1: {id: 1, name: "Tobis Testset", createdAt: Thu Mar 21 2019 17:17:31 GMT+0100 (Central European Standard Time), updatedAt: Thu Mar 21 2019 17:17:31 GMT+0100 (Central European Standard Time), scenarios.id: 2, …}
2: {id: 1, name: "Tobis Testset", createdAt: Thu Mar 21 2019 17:17:31 GMT+0100 (Central European Standard Time), updatedAt: Thu Mar 21 2019 17:17:31 GMT+0100 (Central European Standard Time), scenarios.id: 3, …}
3: {id: 1, name: "Tobis Testset", createdAt: Thu Mar 21 2019 17:17:31 GMT+0100 (Central European Standard Time), updatedAt: Thu Mar 21 2019 17:17:31 GMT+0100 (Central European Standard Time), scenarios.id: 4, …}
4: {id: 2, name: "Carmens Testset", createdAt: Thu Mar 21 2019 17:17:31 GMT+0100 (Central European Standard Time), updatedAt: Thu Mar 21 2019 17:17:31 GMT+0100 (Central European Standard Time), scenarios.id: 5, …}
5: {id: 2, name: "Carmens Testset", createdAt: Thu Mar 21 2019 17:17:31 GMT+0100 (Central European Standard Time), updatedAt: Thu Mar 21 2019 17:17:31 GMT+0100 (Central European Standard Time), scenarios.id: 6, …}
6: {id: 3, name: "Jonas Testset", createdAt: Thu Mar 21 2019 17:17:31 GMT+0100 (Central European Standard Time), upd
方言: mysql 方言版本: 8.0 序列化版本: 5.4.0 使用最新版本进行了测试:是