gpt4 book ai didi

node.js - Bookshelf.js,关联表查询

转载 作者:搜寻专家 更新时间:2023-10-31 22:34:05 27 4
gpt4 key购买 nike

我有一个类似于下面的模型:

var ScholarlyPaper = Bookshelf.Model.extend({

tableName: 'papers',

paragraphs: function() {
return this.hasMany(Paragraph).through(Section);
},

sections: function() {
return this.hasMany(Section);
}

});

var Section = Bookshelf.Model.extend({

tableName: 'sections',

paragraphs: function() {
return this.hasMany(Paragraph);
}

scholarlyPaper: function() {
return this.belongsTo(ScholarlyPaper);
}

});

var Paragraph = Bookshelf.Model.extend({

tableName: 'paragraphs',

section: function() {
return this.belongsTo(Section);
},

scholarlyPaper: function() {
return this.belongsTo(ScholarlyPaper).through(Section);
},

author: function() {
return this.belongsTo(Author);
}

});

var Author = Bookshelf.Model.extend({

tableName: 'authors',

paragraphs: function() {
return this.hasMany(Paragraph);
}

});

使用 Bookshelf.js,给定一个 scholarlyPaper id 和 author id,我怎样才能得到论文中作者没有写一个段落的所有部分?

我面临的特殊挑战是,据我所知,无法在相关表上添加 where 子句(例如“where paragraphs.author_id != author_id”)。

最佳答案

这个有用吗?

new ScholarlyPaper({id: 1}).load({paragraphs: function(qb) {
qb.where('paragraphs.author_id', '!=', author_id);
}}).then(function(paper) {
console.log(JSON.stringify(paper.related('paragraphs')));
});

关于node.js - Bookshelf.js,关联表查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21360879/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com