sequelize update 原生sql 沒有返回值

1.使用sequelize 原生的sql 語句操做,想獲取返回值看看更新結果,可是獲取不到,代碼以下javascript

// 測試修改
    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')
        })
    }

輸出結果java

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 類能夠獲取返回值。mysql

測試代碼以下sql

// 測試修改
    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')
        })
    }

輸出bash

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;

 

【備註】async

1.原始sql ,insert和update 都沒有返回值。測試

相關文章
相關標籤/搜索