2019独角兽企业重金招聘Python工程师标准>>>
1.使用sequelize 原生的sql 语句操作,想获取返回值看看更新结果,但是获取不到,代码如下
// 测试修改
test2: async (req, res, next) => {
await mysql.transaction(async (t) => {
let w = await mysql.query('UPDATE user_wallet_occr SET money = money + :money WHERE id =:id', {
// mysql.query('select * from user_wallet_occr WHERE id =:id', {
replacements: {
money : 1,
id: 'c72c2df6-32dc-4066-94d7-554d09e03400',
},
type: mysql.QueryTypes.UPDATE,
transaction: t
})
if (w){
console.log(w)
}else{
console.log('w is null')
}
res.ok('ok')
}).catch(err => {
console.log('err', err.message)
res.fail('fail')
})
}
输出结果
Executing (76c973f3-f1cd-436f-9653-d1eee2b33660): START TRANSACTION;
Executing (76c973f3-f1cd-436f-9653-d1eee2b33660): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Executing (76c973f3-f1cd-436f-9653-d1eee2b33660): SET autocommit = 1;
Executing (76c973f3-f1cd-436f-9653-d1eee2b33660): UPDATE user_wallet_occr SET money = money + 1 WHERE id ='c72c2df6-32dc-4066-94d7-554d09e03400'
w is null
Executing (76c973f3-f1cd-436f-9653-d1eee2b33660): COMMIT;
2.如果使用 sequelize 的 Model 类可以获取返回值。
测试代码如下
// 测试修改
test2: async (req, res, next) => {
await mysql.transaction(async (t) => {
let w = await UserWalletOccrModel.update({
m: 3
}, {
where: {
id: 'c72c2df6-32dc-4066-94d7-554d09e03400'
},
transaction: t
})
console.log('w1>>',w)
if (w) {
console.log('w>>',JSON.stringify(w))
} else {
console.log('w is null')
}
res.ok('ok')
}).catch(err => {
console.log('err', err.message)
res.fail('fail')
})
}
输出
Executing (8e59d047-00a4-44f6-ac5a-d497f4f0677e): START TRANSACTION;
Executing (8e59d047-00a4-44f6-ac5a-d497f4f0677e): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Executing (8e59d047-00a4-44f6-ac5a-d497f4f0677e): SET autocommit = 1;
Executing (8e59d047-00a4-44f6-ac5a-d497f4f0677e): UPDATE `user_wallet_occr` SET `m`=3 WHERE `id` = 'c72c2df6-32dc-4066-94d7-554d09e03400'
w1>> [ 1 ]
w>> [1]
Executing (8e59d047-00a4-44f6-ac5a-d497f4f0677e): COMMIT;
【备注】
1.原始sql ,insert和update 都没有返回值。