#node.js #sequelize.js #postgresql-9.3
#node.js #sequelize.js #postgresql-9.3
Вопрос:
var promises = [];
promises.push(
sequelize.transaction(function(t1) {
return models.car_service.findAll({
where: sequelize.and(
{ "id_dtc": 6},
{ "id_car": 95 }
),
transaction: t1,
lock: t1.LOCK.UPDATE,
logging: console.log
}).then(function(result) {
console.log(result.dataValues);
if(!result.dataValues){
return models.car_service.create(
{
id_dtc: 6,
id_car: 95,
priority: config.get('apiConfig.coreControllerConfig.createIssueOptions.DTCPriority'),
status: 'new',
dtc_is_pending: false
});
}
})
})
);
promises.push(
sequelize.transaction(function(t2) {
return models.car_service.findAll({
where: sequelize.and(
{ "id_dtc": 6},
{ "id_car": 95 }
),
transaction: t2,
lock: t2.LOCK.UPDATE,
logging: console.log
}).then(function(result) {
console.log(result.dataValues);
if(! result.dataValues){
return models.car_service.create(
{
id_dtc: 6,
id_car: 95,
priority: config.get('apiConfig.coreControllerConfig.createIssueOptions.DTCPriority'),
status: 'new',
dtc_is_pending: false
});
}
})
})
);
return promise.all(promises).then(function(results) {
console.log(results);
});
журнал
Executing (14a1c614-c2f1-46a1-a99f-89bc92ca23dc): SELECT "id", "status", "priority", "mileage", "done_at", "created_at", "updated_at", "id_car", "id_service_type", "id_service_edmunds", "id_service_customized", "id_recall_recallmasters", "id_service_archive", "id_service_mmy", "id_service_preset", "id_dtc", "dtc_is_pending" FROM "car_service" AS "car_service" WHERE ("car_service"."id_dtc" = 6 AND "car_service"."id_car" = 95) FOR UPDATE;
Executing (084558cc-730d-441a-a794-aaa27898db77): SELECT "id", "status", "priority", "mileage", "done_at", "created_at", "updated_at", "id_car", "id_service_type", "id_service_edmunds", "id_service_customized", "id_recall_recallmasters", "id_service_archive", "id_service_mmy", "id_service_preset", "id_dtc", "dtc_is_pending" FROM "car_service" AS "car_service" WHERE ("car_service"."id_dtc" = 6 AND "car_service"."id_car" = 95) FOR UPDATE;
undefined
undefined
Кто-нибудь знает, почему я все еще добавляю 2 одинаковые строки в таблицу, я думал, что блокировка строки должна блокировать транзакцию, у которой есть выбор для обновления?
Ответ №1:
Вы должны передать соответствующий объект транзакции {transaction: t1}
или {transaction: t2}
как вариант во втором запросе транзакций.
Обратитесь к примеру (в документации)