假設我們有一個連接表vehicle_inspections
和另一個連接表inspection_actions
,以及actions
,vehicles, and
檢查的基本表。BookshelfJs未能創建嵌套關係
可以說,我的願望以下DB條目:
vehicles
----------------------------
id make
----------------------------
1 Toyota
actions
-------------------------------
id description
-------------------------------
2 Check Tire Pressue
inspections
-------------------------------
id location date
-------------------------------
3 New York tomorrow
vehicle_inspections
--------------------------------
vehicle_id inspection_id
--------------------------------
1 3
inspection_actions
--------------------------------
inspection_id action_id
--------------------------------
3 2
及以下書架類
inspection_actions.js
(function() {
'use strict';
var Repository = require('../repository');
module.exports = Repository.Model.extend({
tableName: 'inspection_actions',
});
})();
vehicle_inspections.js
(function() {
'use strict';
var Repository = require('../repository');
module.exports = Repository.Model.extend({
tableName = 'vehicle_inspections',
inspection: function() {
return this.belongsTo(require('inspection'));
},
fetchOrCreate: function(vehicleId, inspectionId, options) {
var self = this;
return self.query(function (qb) {
qb.where({
vehicle_id: vehicleId,
inspection_id: inspectionId
});
)}.fetch(options || {}).then(function (model) {
if (!model) {
model.save({
vehicle_id: vehicleId,
inspection_id: inspectionId
});
return model;
};
}
};
});
個inspection.js
...
module.exports = Repository.Model.extend(_.extend({
tableName: 'inspections',
actions: function() {
return this.hasMany(require('./inspection-action'));
}
}));
和路徑:
new VehicleInspection().fetchOrCreate(req.params.vehicle_id, req.params.inspection_id, {withRelated: ['inspection.actions']})
.then(function (vehicleInspection) {
var inspection = vehicleInspection.related('inspection');
console.log(inspection);
console.log(inspection.related(actions);
})
檢查控制檯日誌打印出正確的檢查,但是,他毫無來由是什麼在數據庫中的第二個的console.log打印出一個空結果
{ length: 0,
models: [],
_byId: {},
...
targetIdAttribute: 'id',
foreignKey: undefined,
parentId: undefined,
parentTableName: 'tasks',
parentIdAttribute: 'id',
parentFk: undefined } }
此「不良」行爲僅在首次創建projectTasks條目時發生。看起來正在發生的情況是,inspection_action表沒有通過嵌套的withRelated
填充。我怎麼能得到這個工作嵌套創建工作?
謝謝你提供的信息豐富的答案(我已經upvoted)。 經過一些更深入的挖掘,我已經將問題縮小到了創建上,並適當地修改了我的問題以充分暴露fetchOrCreate並進行詳細說明。 簡短版本是:.fetch按原樣運行,.save如何不創建嵌套關係'inspection.actions',而不是使用正確的條目填充inspection_actions表並因此正確返回空數組。我很樂意聽到你有任何關於解決這個問題的想法:) – 2014-11-23 07:17:17
首先,嘗試vehicleInspection.related('actions')來獲取行動。其次,當您調用fetchOrCreate時,如果由於對象不存在而導致獲取失敗,並且創建它,則返回的模型將僅爲inspection_action模型,並且沒有關係。如果你想要關係,你必須在保存之後再做一次提取。 – 2014-11-24 17:04:59
謝謝:)你是一個學者,一個文雅的女人 – 2014-11-24 18:17:58