Javascript 一对多关系抓取模型及其解析
我有一个细胞同义词的一对多关系(生物学,而不是Excel): Cell.jsJavascript 一对多关系抓取模型及其解析,javascript,bookshelf.js,knex.js,Javascript,Bookshelf.js,Knex.js,我有一个细胞同义词的一对多关系(生物学,而不是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
require('./Synonym');
const model = myBookshelf.Model.extend({
tableName: 'cells',
// Relations
synonyms() {
return this.hasMany('Synonym');
},
});
export const Cell = myBookshelf.model('Cell', model);
同义词
require('./Cell');
const model = myBookshelf.Model.extend({
tableName: 'synonyms',
// Relations
cell() {
return this.belongsTo('Cell');
},
});
export const Synonym = myBookshelf.model('Synonym', model);
我已经覆盖了parse函数来计算每个键
基本模型
...
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)
一切正常。cells对象具有给定单元格的所有同义词。但是,以下代码返回所有同义词,但每个同义词都有一个空单元格键:
let synonyms = await Synonym.fetchAll({ withRelated: ['cell'] });
synonyms = synonyms.toJSON();
如果我删除每个键的camellizing(删除解析函数),此代码将正常工作。我尝试在同义词模型上添加一个cellId
引用(而不是默认的cell\u id
),但没有任何运气。我可以用knex,但我更想弄清楚。我已经研究过重写serialize,但我认为这不是修复此问题的正确位置
查看来自knex的调试日志,单元格是从数据库中检索的,它们只是在获取或序列化过程中丢失了
{ 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' }
如果这有点让人困惑,我很抱歉。如有需要,我可以进一步澄清。谢谢
编辑:
单元格和同义词列表太长,无法显示,因此我在这里展示了一些示例:
cells.toJSON()
同义词.toJSON()
您可以看到,具有同义词的单元格已正确加载,但是每个同义词的单元格键都是空对象,它们应该填充一个单元格。是否可以添加
cells.toJSON()的结果
和同义词.toJSON()代码>?如果我们能准确地看到您在哪里有问题,这会更简单(;请参阅对cells.toJSON();
和同义词.toJSON();
);
。谢谢。您能添加cells.toJSON();
和同义词.toJSON();
的结果吗?如果我们能准确地看到您在哪里有问题,这会更简单(;请参阅对cells.toJSON()的编辑)
和同义词.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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' }
[{
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: []
}]
[{
id: 1,
name: "5637(LCL-1702)",
cellId: 3,
createdAt: "2016-05-09T18:57:04.256Z",
updatedAt: null,
cell: {}
}, {
id: 2,
name: "A-375",
cellId: 6,
createdAt: "2016-05-09T18:57:04.263Z",
updatedAt: null,
cell: {}
}, {
id: 3,
name: "AU-565",
cellId: 11,
createdAt: "2016-05-09T18:57:04.273Z",
updatedAt: null,
cell: {}
}, {
id: 4,
name: "CaSki",
cellId: 19,
createdAt: "2016-05-09T18:57:04.290Z",
updatedAt: null,
cell: {}
}, {
id: 5,
name: "HCC-1395",
cellId: 37,
createdAt: "2016-05-09T18:57:04.340Z",
updatedAt: null,
cell: {}
}]