如何使用knex.js
进行更新语句,其中returning
数据来自多个表。例如,如何用knex.js
编写此更新查询?
update notes
set note = 'This is an updated1 note', timestamp = now()
from(select
id,
note,
user_id,
customer_id
from notes
where id = '46' AND user_id = 7) a
left join customers as b on a.customer_id = b.id
where notes.id = a.id
returning a.id, a.note, a.user_id, b.id;
当只使用一个表时,我可以将其写为返回数据:
knex('notes')
.returning([
'notes.id',
'notes.note',
'notes.user_id AS userId',
'notes.customer_id AS customerId',
'notes.product_id AS productId',
'notes.timestamp',
])
.where('notes.id', noteId)
.update({
timestamp: new Date(),
note: req.body.note,
});
当我尝试介绍customers
时,我的问题就开始了。
我也尝试过:
const [note] = await db.sequelize.knex.raw(
'UPDATE notes '
+ 'SET note = ?, '
+ 'timestamp = NOW() '
+ 'FROM(SELECT id, '
+ 'note, '
+ 'user_id, '
+ 'customer_id '
+ 'FROM notes '
+ 'WHERE id = ? '
+ 'AND user_id = 7) AS a '
+ 'LEFT JOIN customers AS b ON a.customer_id = b.id '
+ 'WHERE notes.id = a.id '
+ 'RETURNING a.id, a.note, a.user_id, b.id', ['This is an updated1 note', 46]
);
但是它给了我TypeError: (intermediate value) is not iterable
。
更新
我现在有了这个:
let note = await db.sequelize.knex.raw(
'UPDATE notes '
+ 'SET note = ?, '
+ 'timestamp = NOW() '
+ 'FROM(SELECT id, '
+ 'note, '
+ 'user_id, '
+ 'customer_id, '
+ 'product_id, '
+ 'timestamp '
+ 'FROM notes '
+ 'WHERE id = ? '
+ 'AND user_id = 7) AS a '
+ 'LEFT JOIN customers AS b ON a.customer_id = b.id '
+ 'LEFT JOIN products AS c ON a.product_id = c.id '
+ 'WHERE notes.id = a.id '
+ 'RETURNING a.id, a.note, a.user_id AS userId, a.customer_id AS customerId, a.product_id AS productId, a.timestamp, b.title AS customerName, b.code AS customerCode, c.title AS productName, c.code AS productCode', [req.body.note, noteId]
);
/*const [note] = await db.sequelize.knex('notes')
.returning([
'notes.id',
'notes.note',
'notes.user_id AS userId',
'notes.customer_id AS customerId',
'notes.product_id AS productId',
'notes.timestamp',
//'customers.title AS customerName',
//'customers.code AS customerCode',
//'products.title AS productName',
//'products.code AS productCode',
])
//.leftJoin('customers', 'notes.customer_id', 'customers.id')
//.leftJoin('products', 'notes.product_id', 'products.id')
.where('notes.id', noteId)
.update({
timestamp: new Date(),
note: req.body.note,
});*/
console.log('PC NOTE', note.rows);
[note] = note.rows;
return res.json({ note });
它返回以下内容:
{
"note": {
"id": 46,
"note": "This is an updated2 note",
"userid": 7,
"customerid": 111781,
"productid": null,
"timestamp": "2019-07-12T19:37:23.996Z",
"customername": "PC CUSTOMER3",
"customercode": "PC003",
"productname": null,
"productcode": null
}
}
这几乎是我所需要的,除了返回的键都是小写字母。我如何使userid
,customerid
,productid
,customername
,customercode
,productname
和productcode
出现在骆驼的情况下就像我在knex语句的RETURN
部分中所说的那样?
答案 0 :(得分:1)
答案 1 :(得分:0)
这对我有用:
let note = await db.sequelize.knex.raw(
'UPDATE notes '
+ 'SET note = ?, '
+ 'timestamp = NOW() '
+ 'FROM(SELECT id, '
+ 'note, '
+ 'user_id, '
+ 'customer_id, '
+ 'product_id, '
+ 'timestamp '
+ 'FROM notes '
+ 'WHERE id = ? '
+ 'AND user_id = 7) AS a '
+ 'LEFT JOIN customers AS b ON a.customer_id = b.id '
+ 'LEFT JOIN products AS c ON a.product_id = c.id '
+ 'WHERE notes.id = a.id '
+ 'RETURNING a.id, '
+ 'notes.note, '
+ 'notes.user_id AS "userId", '
+ 'notes.customer_id AS "customerId", '
+ 'notes.product_id AS "productId", '
+ 'notes.timestamp, '
+ 'b.title AS "customerName", '
+ 'b.code AS "customerCode", '
+ 'c.title AS "productName", '
+ 'c.code AS "productCode"', [newNote, noteId],
);
[note] = note.rows;
return res.json({ note });