所以我试过这个
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
)
我该如何解决这个问题?
答案 0 :(得分:1)
我使用此代码段
修复了它return Promise.all([
user: saveUser(),
book: saveBook()
])
.then(function (result) {
return result.user.books().attach([result.book.id])
})