
async create( userId, title, content, subjectId, type, categoryId, coverImageUrl, tags, published ) { const ctx = this.ctx; let transaction; try { transaction = await ctx.model.transaction(); const post = await ctx.model.Post.create( { author_id: userId, title, content, type, category_id: categoryId, cover_image_url: coverImageUrl, subject_id: subjectId, published, created_at: new Date(), updated_at: null, deleted_at: null, length: 2, render_content: "", subtitle: "", leading_text: "" }, { transaction } ); const postId = post && post.getDataValue("id"); if (!postId) { throw new Error(); } const tagIds = []; for (const tagName of tags) { const result = await ctx.model.Tag.findOrCreate({ where: { name: tagName }, transaction }); tagIds.push(result[0].dataValues["id"]); } for (const tagId of tagIds) { await ctx.model.PostTag.create( { post_id: postId, tag_id: tagId }, { transaction } ); } await transaction.commit(); return { success: true, data: { postId } }; } catch (e) { console.log(e); await transaction.rollback(); return { success: false, message: "服务器异常" }; } } 没有提示错误,接口可以返回 postId,但查数据库后,数据根本没插进去,请问是什么原因呢?
1 zzyyzz1992 2019 年 9 月 21 日 transaction 需要主动提交 |
2 bi531334444 2019 年 9 月 21 日 之前也遇到过,会不会和数据库引擎有关系? |
3 hhh798 OP @zzyyzz1992 我已经写了:await transaction.commit(); 这个不是主动提交? |