我有一对多关系的细胞 - 同义词(生物学,不是Excel):
Cell.js
require('./Synonym');
const model = myBookshelf.Model.extend({
tableName: 'cells',
// Relations
synonyms() {
return this.hasMany('Synonym');
},
});
export const Cell = myBookshelf.model('Cell', model);
Synonym.js
require('./Cell');
const model = myBookshelf.Model.extend({
tableName: 'synonyms',
// Relations
cell() {
return this.belongsTo('Cell');
},
});
export const Synonym = myBookshelf.model('Synonym', model);
我已经覆盖了解析函数以对每个键进行驼峰化。
baseModel
...
const bookshelf = generateBookshelf(knexInstance);
myBookshelf.Model = bookshelf.Model.extend({
...
camelCase(attributes) {
const self = this;
const attrs = {};
_.each(attributes, (value, key) => {
attrs[camelize(key)] = value;
});
return attrs;
},
parse(attributes) {
return this.camelCase(attributes);
}
});
当我尝试查询单元格并获取相关的同义词时(使用ES7 async / await)......
let cells = await Cell.fetchAll({ withRelated: ['synonyms'] });
cells = cells.toJSON();
一切都按预期工作。 cells对象具有给定单元格的所有同义词。但是,以下代码返回所有同义词,但每个都有一个空单元格键:
let synonyms = await Synonym.fetchAll({ withRelated: ['cell'] });
synonyms = synonyms.toJSON();
如果我删除了每个密钥的camelizing(删除了解析函数),则此代码可以正常运行。我尝试添加cellId
引用(而不是默认{{1}在同义词模型上并没有任何运气。我可以使用knex,但我宁愿想出来。我已经考虑过覆盖序列化,但我认为这不是解决这个问题的正确方法。
查看来自knex的调试日志,从数据库中检索单元格,它们只是在获取或序列化过程中的某处丢失。
cell_id
如果这有点令人困惑,我道歉。如果需要,我可以进一步澄清。感谢。
修改
单元格和同义词的列表太长而无法展示,所以我在这里展示了一些例子:
cells.toJSON()
{ method: 'select',
options: {},
bindings: [],
sql: 'select `synonyms`.* from `synonyms`' }
{ method: 'select',
options: {},
bindings:
[
4, 6, 11, 19, 38, 37, 39, 41, 43, 46, 44, 45, 51, 54, 66, 71, 85,
91, 97, 114, 113, 118, 116, 119, 123, 126, 142, 143, 147, 160, 161
],
sql: 'select `cells`.* from `cells` where `cells`.`id` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' }
synonyms.toJSON()
[{
id: 1,
name: "184A1",
lincsId: "LCL-2080",
source: "ATCC",
createdAt: "2016-05-09T18:57:04.044Z",
updatedAt: "2016-05-09T18:57:04.044Z",
synonyms: []
}, {
id: 2,
name: "184B5",
lincsId: "LCL-2081",
source: "ATCC",
createdAt: "2016-05-09T18:57:04.046Z",
updatedAt: "2016-05-09T18:57:04.046Z",
synonyms: []
}, {
id: 3,
name: 5637,
lincsId: "LCL-1702",
source: "ATCC",
createdAt: "2016-05-09T18:57:04.047Z",
updatedAt: "2016-05-09T18:57:04.047Z",
synonyms: [{
id: 1,
name: "5637(LCL-1702)",
cellId: 3,
createdAt: "2016-05-09T18:57:04.256Z",
updatedAt: null
}]
}, {
id: 4,
name: "600MPE",
lincsId: "LCL-2073",
source: "H.S. Smith (California Pacific Medical Center)",
createdAt: "2016-05-09T18:57:04.047Z",
updatedAt: "2016-05-09T18:57:04.047Z",
synonyms: []
}, {
id: 5,
name: "647-V",
lincsId: "LCL-1708",
source: "Leibniz Institute DSMZ-German Collection of Microorganisms and Cell Cultures",
createdAt: "2016-05-09T18:57:04.048Z",
updatedAt: "2016-05-09T18:57:04.048Z",
synonyms: []
}]
您可以看到具有同义词的单元格已正确加载,但每个同义词的单元格键都是空对象,并且应填充单元格。