{"id":21310,"date":"2024-01-02T07:15:25","date_gmt":"2024-01-02T07:15:25","guid":{"rendered":"https:\/\/server.hk\/blog\/21310\/"},"modified":"2024-11-14T10:37:02","modified_gmt":"2024-11-14T02:37:02","slug":"how-to-fix-postgresql-error-code-22001-string_data_right_truncation","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21310\/","title":{"rendered":"\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a22001 &#8211; \u5b57\u4e32\u8cc7\u6599\u53f3\u5074\u622a\u65b7"},"content":{"rendered":"<h1 id=\"how-to-fix-postgresql-error-code-22001-string_data_right_truncation-PxeqsBGgoH\">\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a22001 &#8211; \u5b57\u4e32\u8cc7\u6599\u53f3\u5074\u622a\u65b7<\/h1>\n<p>PostgreSQL \u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u958b\u6e90\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002\u7136\u800c\uff0c\u50cf\u6240\u6709\u8edf\u4ef6\u4e00\u6a23\uff0c\u5b83\u4e5f\u53ef\u80fd\u9047\u5230\u9700\u8981\u89e3\u6c7a\u7684\u932f\u8aa4\u3002\u5176\u4e2d\u4e00\u500b\u932f\u8aa4\u662f PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a22001 &#8211; \u5b57\u4e32\u8cc7\u6599\u53f3\u5074\u622a\u65b7\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u8a0e\u9019\u500b\u932f\u8aa4\u7684\u542b\u7fa9\u4ee5\u53ca\u5982\u4f55\u4fee\u5fa9\u5b83\u3002<\/p>\n<h2 id=\"understanding-postgresql-error-code-22001-PxeqsBGgoH\">\u7406\u89e3 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a22001<\/h2>\n<p>\u5728\u4f7f\u7528 PostgreSQL \u6642\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u932f\u8aa4\u4ee3\u78bc 22001\uff0c\u9019\u8868\u793a\u767c\u751f\u4e86\u5b57\u4e32\u8cc7\u6599\u53f3\u5074\u622a\u65b7\u3002\u7576\u60a8\u5617\u8a66\u5c07\u4e00\u500b\u904e\u9577\u7684\u503c\u63d2\u5165\u6216\u66f4\u65b0\u5230\u4e00\u500b\u5b9a\u7fa9\u7684\u8cc7\u6599\u985e\u578b\u7684\u6b04\u4f4d\u6642\uff0c\u5c31\u6703\u51fa\u73fe\u6b64\u932f\u8aa4\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u500b\u5b9a\u7fa9\u70ba VARCHAR(10) \u7684\u6b04\u4f4d\uff0c\u800c\u60a8\u5617\u8a66\u63d2\u5165\u4e00\u500b\u5305\u542b 15 \u500b\u5b57\u7b26\u7684\u5b57\u4e32\uff0c\u60a8\u5c07\u6703\u9047\u5230 22001 \u932f\u8aa4\u3002PostgreSQL \u5c07\u6703\u622a\u65b7\u8a72\u5b57\u4e32\u4ee5\u7b26\u5408\u5b9a\u7fa9\u7684\u9577\u5ea6\uff0c\u5c0e\u81f4\u8cc7\u6599\u4e1f\u5931\u3002<\/p>\n<h2 id=\"fixing-the-error-PxeqsBGgoH\">\u4fee\u5fa9\u932f\u8aa4<\/h2>\n<p>\u8981\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a22001\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u60a8\u6b63\u5728\u63d2\u5165\u6216\u66f4\u65b0\u7684\u8cc7\u6599\u7b26\u5408\u8a72\u6b04\u4f4d\u7684\u5b9a\u7fa9\u9577\u5ea6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u53ef\u63a1\u53d6\u7684\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-check-column-definitions-PxeqsBGgoH\">1. \u6aa2\u67e5\u6b04\u4f4d\u5b9a\u7fa9<\/h3>\n<p>\u6aa2\u67e5\u767c\u751f\u932f\u8aa4\u7684\u8868\u683c\u7684\u6b04\u4f4d\u5b9a\u7fa9\u3002\u78ba\u4fdd\u6b04\u4f4d\u7684\u8cc7\u6599\u985e\u578b\u548c\u9577\u5ea6\u9069\u5408\u60a8\u8981\u63d2\u5165\u6216\u66f4\u65b0\u7684\u8cc7\u6599\u3002\u5982\u6709\u5fc5\u8981\uff0c\u4fee\u6539\u6b04\u4f4d\u5b9a\u7fa9\u4ee5\u5bb9\u7d0d\u66f4\u9577\u7684\u5b57\u4e32\u3002<\/p>\n<h3 id=\"2-validate-input-data-PxeqsBGgoH\">2. \u9a57\u8b49\u8f38\u5165\u8cc7\u6599<\/h3>\n<p>\u5728\u63d2\u5165\u6216\u66f4\u65b0\u8cc7\u6599\u4e4b\u524d\uff0c\u9a57\u8b49\u8f38\u5165\u4ee5\u78ba\u4fdd\u5176\u4e0d\u8d85\u904e\u6b04\u4f4d\u7684\u5b9a\u7fa9\u9577\u5ea6\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u7a0b\u5f0f\u7d50\u69cb\u6216\u8cc7\u6599\u5eab\u7d04\u675f\u4f86\u5f37\u5236\u57f7\u884c\u6b64\u9a57\u8b49\u3002\u901a\u904e\u9a57\u8b49\u8f38\u5165\uff0c\u60a8\u53ef\u4ee5\u9632\u6b62\u932f\u8aa4\u7684\u767c\u751f\u3002<\/p>\n<h3 id=\"3-use-truncation-functions-PxeqsBGgoH\">3. \u4f7f\u7528\u622a\u65b7\u51fd\u6578<\/h3>\n<p>\u5982\u679c\u60a8\u9700\u8981\u622a\u65b7\u8cc7\u6599\u4ee5\u7b26\u5408\u5b9a\u7fa9\u7684\u9577\u5ea6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 PostgreSQL \u7684\u5167\u5efa\u622a\u65b7\u51fd\u6578\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>LEFT<\/code> \u51fd\u6578\u5f9e\u5b57\u4e32\u7684\u5de6\u5074\u63d0\u53d6\u6307\u5b9a\u6578\u91cf\u7684\u5b57\u7b26\u3002\u901a\u904e\u5728\u63d2\u5165\u6216\u66f4\u65b0\u4e4b\u524d\u622a\u65b7\u8cc7\u6599\uff0c\u60a8\u53ef\u4ee5\u907f\u514d\u932f\u8aa4\u3002<\/p>\n<h3 id=\"4-handle-errors-gracefully-PxeqsBGgoH\">4. \u512a\u96c5\u5730\u8655\u7406\u932f\u8aa4<\/h3>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u53ef\u80fd\u7121\u6cd5\u9632\u6b62\u932f\u8aa4\u7684\u767c\u751f\u3002\u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0c\u91cd\u8981\u7684\u662f\u8981\u512a\u96c5\u5730\u8655\u7406\u932f\u8aa4\u3002\u60a8\u53ef\u4ee5\u5728\u61c9\u7528\u7a0b\u5f0f\u4ee3\u78bc\u4e2d\u6355\u7372\u932f\u8aa4\uff0c\u4e26\u5411\u7528\u6236\u986f\u793a\u6709\u610f\u7fa9\u7684\u6d88\u606f\uff0c\u6307\u793a\u8f38\u5165\u8cc7\u6599\u904e\u9577\u3002\u9019\u6a23\uff0c\u7528\u6236\u53ef\u4ee5\u66f4\u6b63\u8f38\u5165\u4e26\u91cd\u65b0\u5617\u8a66\u3002<\/p>\n<h2 id=\"summary-PxeqsBGgoH\">\u7e3d\u7d50<\/h2>\n<p>\u7e3d\u4e4b\uff0cPostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a22001 &#8211; \u5b57\u4e32\u8cc7\u6599\u53f3\u5074\u622a\u65b7\u767c\u751f\u5728\u60a8\u5617\u8a66\u63d2\u5165\u6216\u66f4\u65b0\u4e00\u500b\u5c0d\u65bc\u6b04\u4f4d\u5b9a\u7fa9\u9577\u5ea6\u904e\u9577\u7684\u503c\u6642\u3002\u8981\u4fee\u5fa9\u6b64\u932f\u8aa4\uff0c\u60a8\u9700\u8981\u6aa2\u67e5\u6b04\u4f4d\u5b9a\u7fa9\u3001\u9a57\u8b49\u8f38\u5165\u8cc7\u6599\u3001\u5728\u5fc5\u8981\u6642\u4f7f\u7528\u622a\u65b7\u51fd\u6578\uff0c\u4e26\u512a\u96c5\u5730\u8655\u7406\u932f\u8aa4\u3002\u9075\u5faa\u9019\u4e9b\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u78ba\u4fdd PostgreSQL \u6578\u64da\u5eab\u5e73\u7a69\u904b\u884c\u800c\u4e0d\u6703\u9047\u5230\u6b64\u932f\u8aa4\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u4e14\u9ad8\u6027\u80fd\u7684 VPS \u8a17\u7ba1\u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8003\u616e <a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002Server.HK \u63d0\u4f9b\u591a\u7a2e\u8a08\u5283\u548c\u512a\u79c0\u7684\u5ba2\u6236\u652f\u6301\uff0c\u70ba\u60a8\u63d0\u4f9b\u4e00\u6d41\u7684 VPS \u8a17\u7ba1\u670d\u52d9\uff0c\u4ee5\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4fee\u5fa9 PostgreSQL &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-21310","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21310","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=21310"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21310\/revisions"}],"predecessor-version":[{"id":24204,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21310\/revisions\/24204"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}