前提是我有以下更新语句使用Sequelize
return models.UnitPlant.findOne(
{
where:
{
id: unitPlantToUpdate.id
},
include: [
{ model: models.ProductType },
{ model: models.VehiclePlant, as: 'Customers' }
]
})
.then(function (unitPlantFromDb) {
return models
.sequelize
.transaction(
{
isolationLevel: models.sequelize.Transaction.ISOLATION_LEVELS.READ_COMMITTED
},
function (t) {
return unitPlantFromDb.update(unitPlantToUpdate, { transaction: t })
.then(function (unitPlantFromDb) {
return unitPlantFromDb.setProductType(unitPlant.ProductType.id, { transaction: t })
})
.then(function (unitPlantFromDb) {
return unitPlantFromDb.setCustomers(unitPlant.customerIds, { transaction: t });
});
})
.then(function (result) {
return output.getSuccessResult(titles.SUCCESS, validationMessages.SUCCESS_ON_UPDATE, UNIT_PLANT)
})
.catch(function (error) {
console.log(error);
return output.getErrorResult(titles.ERROR_ON_UPDATE, validationMessages.ERROR_ON_UPDATE, error.message);
});
});
Unitplant
注意到我与to有一对多的关系Customer
。上面的代码工作正常,但只要 - 更新时 - 我要么只删除或只插入到customers
.
每当两者都发生时,我会收到以下错误:
未处理的拒绝 SequelizeDatabaseError: 请求只能在 LoggedIn 状态下进行,不能在 SentClientRequest 状态下进行
从控制台会发生这种情况:
Executing (783e1de2-5013-43b9-80e9-248dca6d83e1): BEGIN TRANSACTION;
Executing (783e1de2-5013-43b9-80e9-248dca6d83e1): SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
Executing (783e1de2-5013-43b9-80e9-248dca6d83e1): UPDATE [UnitPlants] SET [expiresAt]='2016-06-22',[updatedAt]='2016-06-03 12:57:50.000 +00:00' OUTPUT INSERTED.* WHERE [id] = 15
Executing (783e1de2-5013-43b9-80e9-248dca6d83e1): UPDATE [UnitPlants] SET [ProductTypeId]=1,[updatedAt]='2016-06-03 12:57:50.000 +00:00' OUTPUT INSERTED.* WHERE [id] = 15
Executing (783e1de2-5013-43b9-80e9-248dca6d83e1): SELECT [VehiclePlant].[id], [VehiclePlant].[code], [VehiclePlant].[name], [VehiclePlant].[expiresAt], [VehiclePlant].[createdAt], [VehiclePlant].[upoCustomer], [VehiclePlant].[isExport], [VehiclePlant].[updatedAt], [UnitPlantCustomers].[createdAt] AS [UnitPlantCustomers.createdAt], [UnitPlantCustomers].[updatedAt] AS [UnitPlantCustomers.updatedAt], [UnitPlantCustomers].[UnitPlantId] AS [UnitPlantCustomers.UnitPlantId], [UnitPlantCustomers].[VehiclePlantId] AS [UnitPlantCustomers.VehiclePlantId] FROM [VehiclePlants] AS [VehiclePlant] INNER JOIN [UnitPlantCustomers] AS [UnitPlantCustomers] ON [VehiclePlant].[id] = [UnitPlantCustomers].[VehiclePlantId] AND [UnitPlantCustomers].[UnitPlantId] = 15;
Executing (783e1de2-5013-43b9-80e9-248dca6d83e1): DELETE FROM [UnitPlantCustomers] WHERE [UnitPlantId] = 15 AND [VehiclePlantId] IN (4); SELECT @@ROWCOUNT AS AFFECTEDROWS;
Executing (783e1de2-5013-43b9-80e9-248dca6d83e1): INSERT INTO [UnitPlantCustomers] ([createdAt],[updatedAt],[UnitPlantId],[VehiclePlantId]) VALUES ('2016-06-03 12:57:50.000 +00:00','2016-06-03 12:57:50.000 +00:00',15,3);
Executing (783e1de2-5013-43b9-80e9-248dca6d83e1): ROLLBACK TRANSACTION;
注意那里的INSERT和DELETE。
为什么只有当我两者都有时才会发生这种情况?我该如何解决这个问题?
顺便说一句,当我不使用事务时,整个事情都有效,这让我想知道我是否选择了错误的隔离级别,或者我的整个事务设置是错误的。