Sequelize 交易和增量
Sequelize Transactions and Increments
我在使用 sequelize.transaction
和 model.increment
时遇到问题
我有这个简单的模型
class Wallet extends Model {}
Wallet.init(
{
// Model attributes are defined here
id: {
type: DataTypes.INTEGER.UNSIGNED,
autoIncrement: true,
primaryKey: true,
allowNull: false,
},
availableBalance: {
type: DataTypes.FLOAT,
allowNull: false,
defaultValue: 0,
},
}
);
我尝试这样做:
const wallet = await Wallet.findById(1);
await sequelize.transaction(async t => {
//more sentences
wallet.increment('availableBalance', { by: 1, transaction: t });
});
失败的是:
Error: commit has been called on this
transaction(6de20ad2-ab57-4038-9003-fabff6437449), you can no longer
use it.
注意:交易里面的语句比较多,我只留下了冲突的那一行(增量语句)
有什么想法吗?
您忘记等待异步 increment
调用:
const wallet = await Wallet.findById(1);
await sequelize.transaction(async t => {
//more sentences
await wallet.increment('availableBalance', { by: 1, transaction: t });
});
我在使用 sequelize.transaction
和 model.increment
我有这个简单的模型
class Wallet extends Model {}
Wallet.init(
{
// Model attributes are defined here
id: {
type: DataTypes.INTEGER.UNSIGNED,
autoIncrement: true,
primaryKey: true,
allowNull: false,
},
availableBalance: {
type: DataTypes.FLOAT,
allowNull: false,
defaultValue: 0,
},
}
);
我尝试这样做:
const wallet = await Wallet.findById(1);
await sequelize.transaction(async t => {
//more sentences
wallet.increment('availableBalance', { by: 1, transaction: t });
});
失败的是:
Error: commit has been called on this transaction(6de20ad2-ab57-4038-9003-fabff6437449), you can no longer use it.
注意:交易里面的语句比较多,我只留下了冲突的那一行(增量语句)
有什么想法吗?
您忘记等待异步 increment
调用:
const wallet = await Wallet.findById(1);
await sequelize.transaction(async t => {
//more sentences
await wallet.increment('availableBalance', { by: 1, transaction: t });
});