{"id":200294,"date":"2025-05-06T08:25:06","date_gmt":"2025-05-06T00:25:06","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200294\/"},"modified":"2025-05-06T08:25:06","modified_gmt":"2025-05-06T00:25:06","slug":"sequelize-%e4%ba%8b%e5%8a%a1%e5%9b%9e%e6%bb%9a%e5%a4%b1%e8%b4%a5%ef%bc%8c%e6%97%a5%e5%bf%97%e6%98%be%e7%a4%ba%e5%b7%b2%e6%89%a7%e8%a1%8c%e4%bd%86%e6%8f%92%e5%85%a5%e7%9a%84%e8%ae%b0%e5%bd%95%e4%bb%8d","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200294\/","title":{"rendered":"Sequelize \u4e8b\u52a1\u56de\u6eda\u5931\u8d25\uff0c\u65e5\u5fd7\u663e\u793a\u5df2\u6267\u884c\u4f46\u63d2\u5165\u7684\u8bb0\u5f55\u4ecd\u5b58\u5728\uff0c\u95ee\u9898\u51fa\u5728\u54ea\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Sequelize \u4e8b\u52a1\u56de\u6eda\u5931\u8d25\uff0c\u65e5\u5fd7\u663e\u793a\u5df2\u6267\u884c\u4f46\u63d2\u5165\u7684\u8bb0\u5f55\u4ecd\u5b58\u5728\uff0c\u95ee\u9898\u51fa\u5728\u54ea\uff1f<\/h1>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6570\u636e\u5e93\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300aSequelize \u4e8b\u52a1\u56de\u6eda\u5931\u8d25\uff0c\u65e5\u5fd7\u663e\u793a\u5df2\u6267\u884c\u4f46\u63d2\u5165\u7684\u8bb0\u5f55\u4ecd\u5b58\u5728\uff0c\u95ee\u9898\u51fa\u5728\u54ea\uff1f\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241031\/17303675786723505ad0e64.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>node.js \u4e2d\u4f7f\u7528 sequelize \u6267\u884c\u4e8b\u52a1\u56de\u6eda\u65f6\u906d\u9047\u95ee\u9898<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 sequelize \u8fdb\u884c\u4e8b\u52a1\u5904\u7406\u65f6\uff0c\u60a8\u9047\u5230\u4e86\u56de\u6eda\u5931\u8d25\u7684\u95ee\u9898\uff0c\u5c3d\u7ba1\u65e5\u5fd7\u663e\u793a\u56de\u6eda\u5df2\u6267\u884c\uff0c\u4f46\u63d2\u5165\u7684\u8bb0\u5f55\u4ecd\u5b58\u5728\u3002<\/p>\n<p><strong>\u95ee\u9898\u5206\u6790<\/strong><\/p>\n<p>\u6839\u636e\u60a8\u63d0\u4f9b\u7684\u4ee3\u7801\uff0c\u60a8\u5df2\u6b63\u786e\u8bbe\u7f6e\u4e86\u4e8b\u52a1 (t) \u5e76\u5c06\u5176\u4f20\u9012\u7ed9 create() \u65b9\u6cd5\u3002\u4f46\u662f\uff0c\u5b58\u5728\u4e00\u4e2a\u5173\u952e\u95ee\u9898\uff1a<\/p>\n<p><strong>\u8868\u5b58\u50a8\u5f15\u64ce<\/strong><\/p>\n<p>\u60a8\u672a\u6307\u5b9a\u8868\u5b58\u50a8\u5f15\u64ce\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cmysql \u4f7f\u7528 myisam \u5b58\u50a8\u5f15\u64ce\uff0c\u4e0d\u652f\u6301\u4e8b\u52a1\u3002\u8981\u542f\u7528\u4e8b\u52a1\uff0c\u9700\u8981\u5c06\u8868\u5b58\u50a8\u5f15\u64ce\u66f4\u6539\u4e3a innodb\uff1a<\/p>\n<pre>alter table groups engine=innodb;<\/pre>\n<p><strong>\u4fee\u6539\u540e\u7684\u4ee3\u7801<\/strong><\/p>\n<p>\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>\/\/ \u521b\u5efa\u7fa4\u7ec4\nexports.createGroup = async function (user_id, name, img_url) {\n    \/\/ \u521b\u5efa\u4e8b\u52a1\n    const t = await sequelize.transaction();\n\n    try {\n        \/\/ \u521b\u5efa\u7fa4\u7ec4\u8bb0\u5f55\n        let result = await models(sequelize).groups.create({\n            user_id: user_id,\n            name: name,\n            img_url: img_url\n        },{ transaction: t });\n\n        \/\/ \u56de\u6eda\u4e8b\u52a1\n        await t.rollback();\n\n        console.log('\u56de\u6eda');\n\n        return func.resJsonSuccess(result, '\u5efa\u7fa4\u6210\u529f!');\n\n    } catch (error) {\n        return func.resJsonError([], error.message);\n    }\n}<\/pre>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u5c06\u8868\u5b58\u50a8\u5f15\u64ce\u66f4\u6539\u4e3a innodb \u5373\u53ef\u89e3\u51b3\u6b64\u95ee\u9898\u3002\u91cd\u65b0\u8fd0\u884c\u4ee3\u7801\uff0c\u60a8\u5e94\u8be5\u53ef\u4ee5\u6210\u529f\u56de\u6eda\u63d2\u5165\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aSequelize \u4e8b\u52a1\u56de\u6eda\u5931\u8d25\uff0c\u65e5\u5fd7\u663e\u793a\u5df2\u6267\u884c\u4f46\u63d2\u5165\u7684\u8bb0\u5f55\u4ecd\u5b58\u5728\uff0c\u95ee\u9898\u51fa\u5728\u54ea\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Sequelize \u4e8b\u52a1\u56de\u6eda\u5931\u8d25&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200294","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200294"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200294\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}