Я создаю приложение с помощью Express, и я использую книжную полку (mysql) js в качестве ORM.
У меня есть отношение многие ко многим, и я пытаюсь сохранить в соединительной таблице некоторые дополнительные данные.
Мои модели:
const Product = Bookshelf.Model.extend({
tableName: 'products',
factors() {
return this.belongsToMany('Factor', 'product_factors', 'product_id', 'factor_id')
}
})
const Factor = Bookshelf.Model.extend({
tableName: 'factors',
products() {
return this.belongsToMany('Product')
}
})
Создание нового продукта со связями в таблице product_factors.
/** Create product */
router.route('/').post((req, res) => {
new Product({name: req.body.name})
.save()
.then(product => {
// attaching relation
product.factors().attach([3, 5])
.then(hz => {
res.status(201).json({
message: 'Product created'
})
})
})
})
Таблица «product_factors»
|ID|product_id|factor_id|quantity|
|--|----------|---------|--------|
|1 |10 |3 |NULL |
|2 |10 |5 |NULL |
Как я могу вставить количество одновременно с прикрепленными отношениями?