{"id":200295,"date":"2025-05-06T11:38:05","date_gmt":"2025-05-06T03:38:05","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200295\/"},"modified":"2025-05-06T11:38:05","modified_gmt":"2025-05-06T03:38:05","slug":"node-js%e4%b8%adsequelize%e4%ba%8b%e5%8a%a1%e5%9b%9e%e6%bb%9a%e5%a4%b1%e8%b4%a5%ef%bc%9a%e5%a6%82%e4%bd%95%e7%a1%ae%e4%bf%9d%e6%95%b0%e6%8d%ae%e5%ba%93%e6%93%8d%e4%bd%9c%e8%a2%ab%e6%92%a4%e9%94%80","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200295\/","title":{"rendered":"Node.js\u4e2dSequelize\u4e8b\u52a1\u56de\u6eda\u5931\u8d25\uff1a\u5982\u4f55\u786e\u4fdd\u6570\u636e\u5e93\u64cd\u4f5c\u88ab\u64a4\u9500\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Node.js\u4e2dSequelize\u4e8b\u52a1\u56de\u6eda\u5931\u8d25\uff1a\u5982\u4f55\u786e\u4fdd\u6570\u636e\u5e93\u64cd\u4f5c\u88ab\u64a4\u9500\uff1f<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241031\/1730367948672351ccbe807.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5728 node.js \u4e2d\u4f7f\u7528 sequelize \u8fdb\u884c\u4e8b\u52a1\u56de\u6eda<\/strong><\/p>\n<p>\u5728\u5c1d\u8bd5\u4f7f\u7528 sequelize \u6267\u884c\u4e8b\u52a1\u56de\u6eda\u65f6\uff0c\u67e5\u8be2\u64cd\u4f5c\u53ef\u80fd\u5e76\u672a\u771f\u6b63\u64a4\u6d88\u3002\u4e0b\u6587\u4ecb\u7ecd\u4e86\u6b64\u95ee\u9898\u53ef\u80fd\u7684\u539f\u56e0\u548c\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p><strong>\u95ee\u9898\u539f\u56e0<\/strong><\/p>\n<p>\u5bfc\u81f4\u4e8b\u52a1\u56de\u6eda\u5931\u8d25\u7684\u539f\u56e0\u4e0e\u6570\u636e\u5e93\u7684\u5b58\u50a8\u5f15\u64ce\u6709\u5173\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cmysql \u4e2d\u7684 innodb \u8868\u652f\u6301\u4e8b\u52a1\u5904\u7406\uff0c\u800c myisam \u8868\u4e0d\u652f\u6301\u3002\u5982\u679c\u4f7f\u7528\u7684\u8868\u4e0d\u662f innodb \u8868\uff0c\u5219\u56de\u6eda\u64cd\u4f5c\u5c06\u4e0d\u8d77\u4f5c\u7528\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u9700\u5c06\u8868\u5b58\u50a8\u5f15\u64ce\u66f4\u6539\u4e3a innodb\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u8fde\u63a5\u5230 mysql \u6570\u636e\u5e93\u3002<\/li>\n<li>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\u8868\u7684\u5b58\u50a8\u5f15\u64ce\uff1a<\/li>\n<\/ol>\n<pre>show table status like 'your_table_name'<\/pre>\n<ol>\n<li>\u5982\u679c\u5b58\u50a8\u5f15\u64ce\u4e0d\u662f innodb\uff0c\u8bf7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u5c06\u5176\u66f4\u6539\u4e3a innodb\uff1a<\/li>\n<\/ol>\n<pre>alter table your_table_name engine=innodb<\/pre>\n<ol>\n<li>\u786e\u8ba4\u5b58\u50a8\u5f15\u64ce\u5df2\u66f4\u6539\u4e3a innodb\uff1a<\/li>\n<\/ol>\n<pre>show table status like 'your_table_name'<\/pre>\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<p>\u4fee\u6539\u8868\u7684\u5b58\u50a8\u5f15\u64ce\uff1a<\/p>\n<pre>alter table groups engine=innodb;<\/pre>\n<p>\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5e94\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>exports.createGroup = async function (user_id, name, img_url) {\n  const t = await sequelize.transaction();\n  try {\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    await t.rollback();\n    console.log('\u56de\u6eda');\n    return func.resJsonSuccess(result, '\u5efa\u7fa4\u6210\u529f!');\n  } catch (error) {\n    return func.resJsonError([], error.message);\n  }\n};<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aNode.js\u4e2dSequelize\u4e8b\u52a1\u56de\u6eda\u5931\u8d25\uff1a\u5982\u4f55\u786e\u4fdd\u6570\u636e\u5e93\u64cd\u4f5c\u88ab\u64a4\u9500\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6570\u636e\u5e93\u77e5\u8bc6\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Node.js\u4e2dSequeliz&#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-200295","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200295","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=200295"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200295\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}