admin管理员组文章数量:1414613
I am inserting a note into the quoteNotes table. When I insert it and console log the res. it doesn't give me a record of the inserted note.
router.post('/:id/notes', (req, res) => {
const {id} = req.params;
const note = req.body;
note.quote_id = id
// if(note.note === "") return res.status(422)
// .json({message: 'Note cannot be empty'});
Quotes.addNote(note).then((quoteNote) => {
console.log(quoteNote)
res.status(200).json(quoteNote);
});
});
console.log =>
Result {
mand: 'INSERT',
rowCount: 1,
oid: 0,
rows: [],
fields: [],
_parsers: undefined,
_types: TypeOverrides {
_types: {
getTypeParser: [Function: getTypeParser],
setTypeParser: [Function: setTypeParser],
arrayParser: [Object],
builtins: [Object]
},
text: {},
binary: {}
},
RowCtor: null,
rowAsArray: false
}
I am inserting a note into the quoteNotes table. When I insert it and console log the res. it doesn't give me a record of the inserted note.
router.post('/:id/notes', (req, res) => {
const {id} = req.params;
const note = req.body;
note.quote_id = id
// if(note.note === "") return res.status(422)
// .json({message: 'Note cannot be empty'});
Quotes.addNote(note).then((quoteNote) => {
console.log(quoteNote)
res.status(200).json(quoteNote);
});
});
console.log =>
Result {
mand: 'INSERT',
rowCount: 1,
oid: 0,
rows: [],
fields: [],
_parsers: undefined,
_types: TypeOverrides {
_types: {
getTypeParser: [Function: getTypeParser],
setTypeParser: [Function: setTypeParser],
arrayParser: [Object],
builtins: [Object]
},
text: {},
binary: {}
},
RowCtor: null,
rowAsArray: false
}
Share
Improve this question
asked Jan 21, 2022 at 21:12
Tim BogdanovTim Bogdanov
2405 silver badges25 bronze badges
3 Answers
Reset to default 3Figured it out.
needed to add .returning('id')
to the query.
function addNote(data) {
return db('quote_notes')
.insert(data)
.returning('id');
}
In my case with mysql, the await insert({...})
returned an array with an id as a single element. And by using returning('id')
I got an error saying it's not supported for mysql.
For anyone else that lands here, below is a (clumsy) approach you can use to return a non auto-increment insert ID e.g. a uuid()
from a MySQL database using Knex.
In our case, the insert query is being built by Knex, with raw SQL added before the database is queried with MySQL2.
// Assumes your primary key column is 'id'
const query = knex('table_name').insert({
...data,
id: knex.raw('@id'),
}).onConflict('id').merge().toSQL();
const [[setId, checkId, insert, [{ lastId }]]] = await connection.query('SET @id = ?; SET @id = IFNULL(@id, UUID());' + query.sql + '; SELECT cast(@id as CHAR) AS lastId;', [data.id, ...query.bindings]);
console.log(lastId)
multipleStatements
or your equivalent (see here) must be enabled for this to work - make sure everything is properly escaped!!
本文标签: javascriptKnexjs return inserted record IDStack Overflow
版权声明:本文标题:javascript - Knex.js return inserted record ID - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745191194a2646918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论