gpt4 book ai didi

sequelize.js - sequelize 事务 - 请求只能在 LoggedIn 状态下进行,不能在 SentClientRequest 状态下进行

转载 作者:行者123 更新时间:2023-12-03 22:20:28 30 4
gpt4 key购买 nike

前提是我有以下使用 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);
});
});

注意到我在 UnitplantCustomer 上有一对多的关系。
上面的代码工作正常,但只要 - 更新时 - 我要么只 删除 要么只 插入 customers

每当两者发生时,我都会收到以下错误:

未处理的拒绝 SequelizeDatabaseError: Requests can only be made in the LoggedIn state, not the SentClientRequest state

从控制台发生这种情况:
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 那里。

为什么只有当我两者都有时才会发生这种情况?我该如何解决这个问题?

仍然作为旁注,当我 使用事务时,整个事情都会起作用,这让我想知道是我选择了错误的隔离级别还是我的整个事务设置是错误的。

最佳答案

默认情况下乏味(节点 mssql 驱动程序)仅支持一个并发请求,而其他数据库库自动管理查询队列。应该在 Sequelize 3.22 版中修复

https://github.com/sequelize/sequelize/pull/5752

关于sequelize.js - sequelize 事务 - 请求只能在 LoggedIn 状态下进行,不能在 SentClientRequest 状态下进行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37615174/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com