[英]How to interact with a join table in bookshelf and knex?
I want to create an entry in a join table using bookshelf.我想使用书架在连接表中创建一个条目。 I followed the instructions on the bookshelf website here .我按照此处书架网站上的说明进行操作。 (Models instantiation with bookshelf and tables creation with knex). (模型实例化与书架和表创建与 knex)。
Is there a way, upon creation of a new Book, to add values to the authors_books table WITHOUT creating an authors_books model/collection?在创建新书时,有没有办法在不创建authors_books 模型/集合的情况下向authors_books 表添加值?
What should I add to this:我应该添加什么:
//Books references the Books collection that holds the Book models
var book = new Book({
title: title,
base_url: req.headers.origin
});
book.save().then(function(newBook) {
Books.add(newLink);
res.send(200, newBook);
});
Yes, of course, it's quite simple.是的,当然,这很简单。
You need two models, Book and Author.您需要两个模型,Book 和 Author。 Both have belongsToMany
relationship defined and withPivot
as well.两者都定义了belongsToMany
关系和withPivot
。 withPivot
lists the attributes that are in the junction table, see more here: http://bookshelfjs.org/#Model-withPivot withPivot
列出了连接表中的属性,请在此处查看更多信息: http : //bookshelfjs.org/#Model-withPivot
Afterwards, you call updatePivot
, to update the attributes that are in author_book
table only.之后,您调用updatePivot
以仅更新author_book
表中的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.