我有一个带有身份 ID 的表。每当将新记录插入给定表时,此值都会自动增加。但是,我无法返回插入的 ID 以响应我的查询。
module.exports = sequelize.define('MyTable', {
aField1: Sequelize.STRING,
aField2: Sequelize.STRING,
id: { type: Sequelize.BIGINT, primaryKey: true, autoIncrement: true }
})
通过调用
create()
函数将项目正确插入到数据库中,但执行的查询没有返回任何 ID。日志告诉我以下内容:Executing (fdfd7f79d028c57c1c6f):
INSERT INTO [dbo].[MyTable] ([aField1], [aField2])
OUTPUT INSERTED.* VALUES ('aValue1', 'aValue2');
如何在此 Sequelize 查询中返回插入的 ID?
谢谢您的帮助!
最佳答案
据我所知,您只想发送 id 作为响应。
代码应该看起来像
`.post((req, res, next) => {
DATABASE_NAME.create(req.body)
.then((result) => {
//console.log(result);
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(result.id);
}, (err) => next(err))
.catch((err) => next(err));
})`
关于sql-server - 如何使用 SequelizeJS 从表中返回非常插入的 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48169314/