sequelize 在没有模型的情况下查找和更新记录
sequelize find and update a record without a model
我想使用 sequelize.js 和 mysql2 更新数据库记录
我无权访问模型文件夹,或者它们尚未制作,所以有没有办法更新找不到解决方案所有解决方案都是我检查过的模型名称点更新
var Book = db.define(‘books’, {
title: {
type: Sequelize.STRING
},
pages: {
type: Sequelize.INTEGER
}
})
Book.update(
{title: req.body.title},
{returning: true, where: {id: req.params.bookId} }
)
.then(function([ rowsUpdate, [updatedBook] ]) {
res.json(updatedBook)
})
.catch(e => console.log(e));
我想要你的专业解决方案
有关基于版本的最新文档,请参阅 Sequelize ORM。
更新 - v6
您可以同时使用 query model(preferred)
和 raw queries
来实现这一点。
// Using query model(User)
await User.update({ y: 42 }, {
where: {
x: 12
}
});
// Using raw query
const [results, metadata] = await sequelize.query("UPDATE users SET y = 42 WHERE x = 12");
// Results will be an empty array and metadata will contain the number of affected rows.
旧 - v3
sequelize.query("UPDATE users SET y = 42 WHERE x = 12").spread(function(results, metadata) {
// Results will be an empty array and metadata will contain the number of affected rows.
})
我想使用 sequelize.js 和 mysql2 更新数据库记录 我无权访问模型文件夹,或者它们尚未制作,所以有没有办法更新找不到解决方案所有解决方案都是我检查过的模型名称点更新
var Book = db.define(‘books’, {
title: {
type: Sequelize.STRING
},
pages: {
type: Sequelize.INTEGER
}
})
Book.update(
{title: req.body.title},
{returning: true, where: {id: req.params.bookId} }
)
.then(function([ rowsUpdate, [updatedBook] ]) {
res.json(updatedBook)
})
.catch(e => console.log(e));
我想要你的专业解决方案
有关基于版本的最新文档,请参阅 Sequelize ORM。
更新 - v6
您可以同时使用 query model(preferred)
和 raw queries
来实现这一点。
// Using query model(User)
await User.update({ y: 42 }, {
where: {
x: 12
}
});
// Using raw query
const [results, metadata] = await sequelize.query("UPDATE users SET y = 42 WHERE x = 12");
// Results will be an empty array and metadata will contain the number of affected rows.
旧 - v3
sequelize.query("UPDATE users SET y = 42 WHERE x = 12").spread(function(results, metadata) {
// Results will be an empty array and metadata will contain the number of affected rows.
})