如何与Bookshelfjs保存1到多个关系

所以我试了这个

let Book = bookshelf.Model.extend({tableName: 'book'}) let User = bookshelf.Model.extend({ tableName: 'user', books: function() { return this.belongsToMany(Book) // i have table book_user with user_id and book_id fk to respective tables } }) return User 

然后,我试图保存使用此片段

 models.User.forge(myUserObject).save() .then(function (user) { return user.books().attach(myBookArray) }) 

但它没有工作,因为它期望的关系对象(表book_user

我该如何解决?

我使用这个片段修复了它

 return Promise.all([ user: saveUser(), book: saveBook() ]) .then(function (result) { return result.user.books().attach([result.book.id]) })