Sequelize Postgres交易问题

时间:2019-11-27 11:15:47

标签: node.js postgresql sequelize.js

我有一个表配置文件,在profileId和provider字段上有复合唯一键。我正在使用模型在个人档案表中创建记录,代码如下

async function createUserProfile(userProfile){
   let transaction;
   try{
      transaction = await sequelize.transaction();
      const hyperProfile = await UserHyperProfiles.create(userProfile, {transaction:transaction});
      console.log("profile cretaed");
      transaction.commit();
   }catch(error){
     if(transaction){
       transaction.rollback()
     }
     console.log(error);
   }
}

使用上面的代码,即使我没有任何记录,它也会给我带来独特的约束错误

  SequelizeUniqueConstraintError : Key (provider, profileid)=(google, f123) already exists.

但是当我从create调用中删除事务时,它工作正常。 有什么我想念的吗?。

0 个答案:

没有答案