{"id":21332,"date":"2024-01-02T07:37:26","date_gmt":"2024-01-02T07:37:26","guid":{"rendered":"https:\/\/server.hk\/blog\/21332\/"},"modified":"2024-11-14T10:36:35","modified_gmt":"2024-11-14T02:36:35","slug":"how-to-fix-postgresql-error-code-22037-non_unique_keys_in_a_json_object","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21332\/","title":{"rendered":"\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a22037 &#8211; non_unique_keys_in_a_json_object"},"content":{"rendered":"<h1 id=\"how-to-fix-postgresql-error-code-22037-non_unique_keys_in_a_json_object-cNWbrYtTmN\">\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a22037 &#8211; non_unique_keys_in_a_json_object<\/h1>\n<p>PostgreSQL \u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u958b\u6e90\u95dc\u806f\u5f0f\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5f0f\u4e2d\u3002\u5b83\u63d0\u4f9b\u4e86\u8a31\u591a\u529f\u80fd\uff0c\u5305\u62ec\u5c0d JSON \u8cc7\u6599\u985e\u578b\u7684\u652f\u6301\u3002\u7136\u800c\uff0c\u8207\u5176\u4ed6\u8edf\u4ef6\u4e00\u6a23\uff0cPostgreSQL \u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u932f\u8aa4\u548c\u554f\u984c\u9700\u8981\u89e3\u6c7a\u3002\u5176\u4e2d\u4e00\u500b\u932f\u8aa4\u5c31\u662f PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a22037 &#8211; non_unique_keys_in_a_json_object\u3002<\/p>\n<h2 id=\"understanding-the-error-cNWbrYtTmN\">\u7406\u89e3\u932f\u8aa4<\/h2>\n<p>PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a22037 &#8211; non_unique_keys_in_a_json_object \u6703\u5728\u60a8\u5617\u8a66\u63d2\u5165\u6216\u66f4\u65b0\u4e00\u500b\u5305\u542b\u91cd\u8907\u9375\u7684 JSON \u5c0d\u8c61\u6642\u767c\u751f\u3002\u5728 PostgreSQL \u4e2d\uff0cJSON \u5c0d\u8c61\u662f\u4ee5\u9375\u503c\u5c0d\u7684\u5f62\u5f0f\u8868\u793a\u7684\uff0c\u4e14\u6bcf\u500b\u9375\u5728\u5c0d\u8c61\u4e2d\u5fc5\u9808\u662f\u552f\u4e00\u7684\u3002\u5982\u679c\u60a8\u5617\u8a66\u63d2\u5165\u6216\u66f4\u65b0\u4e00\u500b\u542b\u6709\u91cd\u8907\u9375\u7684 JSON \u5c0d\u8c61\uff0cPostgreSQL \u4fbf\u6703\u62cb\u51fa\u6b64\u932f\u8aa4\u3002<\/p>\n<h2 id=\"resolving-the-error-cNWbrYtTmN\">\u89e3\u6c7a\u932f\u8aa4<\/h2>\n<p>\u8981\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a22037 &#8211; non_unique_keys_in_a_json_object\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u60a8\u7684 JSON \u5c0d\u8c61\u5177\u6709\u552f\u4e00\u7684\u9375\u3002\u4ee5\u4e0b\u662f\u5e7e\u500b\u6b65\u9a5f\u5e6b\u52a9\u60a8\u89e3\u6c7a\u6b64\u554f\u984c\uff1a<\/p>\n<h3 id=\"1-identify-the-json-object-cNWbrYtTmN\">1. \u8b58\u5225 JSON \u5c0d\u8c61<\/h3>\n<p>\u9996\u5148\uff0c\u8b58\u5225\u5c0e\u81f4\u932f\u8aa4\u7684 JSON \u5c0d\u8c61\u3002\u67e5\u627e\u89f8\u767c\u932f\u8aa4\u8a0a\u606f\u7684\u5177\u9ad4\u67e5\u8a62\u6216\u64cd\u4f5c\u3002\u9019\u5c07\u5e6b\u52a9\u60a8\u7e2e\u5c0f\u554f\u984c\u7bc4\u570d\uff0c\u4e26\u96c6\u4e2d\u7cbe\u529b\u89e3\u6c7a\u554f\u984c\u3002<\/p>\n<h3 id=\"2-check-for-duplicate-keys-cNWbrYtTmN\">2. \u6aa2\u67e5\u91cd\u8907\u9375<\/h3>\n<p>\u7576\u60a8\u8b58\u5225\u51fa\u554f\u984c\u7684 JSON \u5c0d\u8c61\u5f8c\uff0c\u4ed4\u7d30\u6aa2\u67e5\u5176\u9375\u503c\u5c0d\uff0c\u5c0b\u627e\u5c0d\u8c61\u4e2d\u662f\u5426\u5b58\u5728\u4efb\u4f55\u91cd\u8907\u9375\u3002\u6bcf\u500b\u9375\u61c9\u8a72\u53ea\u51fa\u73fe\u4e00\u6b21\u3002\u5982\u679c\u767c\u73fe\u91cd\u8907\u9375\uff0c\u8acb\u522a\u9664\u6216\u91cd\u547d\u540d\u5b83\u5011\uff0c\u4ee5\u78ba\u4fdd\u9375\u7684\u552f\u4e00\u6027\u3002<\/p>\n<h3 id=\"3-use-arrays-or-nested-objects-cNWbrYtTmN\">3. \u4f7f\u7528\u9663\u5217\u6216\u5d4c\u5957\u5c0d\u8c61<\/h3>\n<p>\u5982\u679c\u60a8\u9700\u8981\u5728\u540c\u4e00\u9375\u4e0b\u5b58\u5132\u591a\u500b\u503c\uff0c\u8acb\u8003\u616e\u4f7f\u7528\u9663\u5217\u6216\u5d4c\u5957\u5c0d\u8c61\uff0c\u800c\u4e0d\u662f\u55ae\u4e00\u7684 JSON \u5c0d\u8c61\u3002\u9663\u5217\u5141\u8a31\u60a8\u5728\u540c\u4e00\u9375\u4e0b\u5b58\u5132\u591a\u500b\u503c\uff0c\u800c\u5d4c\u5957\u5c0d\u8c61\u5247\u63d0\u4f9b\u4e86\u4e00\u7a2e\u5206\u5c64\u7d50\u69cb\u4f86\u7d44\u7e54\u60a8\u7684\u6578\u64da\u3002<\/p>\n<h3 id=\"4-update-the-query-or-operation-cNWbrYtTmN\">4. \u66f4\u65b0\u67e5\u8a62\u6216\u64cd\u4f5c<\/h3>\n<p>\u7576\u60a8\u89e3\u6c7a\u4e86\u91cd\u8907\u9375\u7684\u554f\u984c\u5f8c\uff0c\u8acb\u66f4\u65b0\u5c0e\u81f4\u932f\u8aa4\u7684\u67e5\u8a62\u6216\u64cd\u4f5c\u3002\u78ba\u4fdd\u4f7f\u7528\u6c92\u6709\u91cd\u8907\u9375\u7684\u6b63\u78ba JSON \u5c0d\u8c61\u3002\u6e2c\u8a66\u66f4\u65b0\u5f8c\u7684\u67e5\u8a62\u6216\u64cd\u4f5c\uff0c\u4ee5\u78ba\u4fdd\u932f\u8aa4\u4e0d\u518d\u767c\u751f\u3002<\/p>\n<h2 id=\"example-cNWbrYtTmN\">\u7bc4\u4f8b<\/h2>\n<p>\u8b93\u6211\u5011\u901a\u904e\u4e00\u500b\u7bc4\u4f8b\u4f86\u8aaa\u660e\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a22037 &#8211; non_unique_keys_in_a_json_object\u3002\u5047\u8a2d\u60a8\u6709\u4e00\u500b\u8868\u793a\u67d0\u4eba\u8a73\u7d30\u8cc7\u8a0a\u7684 JSON \u5c0d\u8c61\uff1a<\/p>\n<pre><code>{\r\n  \"name\": \"John Doe\",\r\n  \"age\": 30,\r\n  \"email\": \"john@example.com\",\r\n  \"email\": \"johndoe@example.com\"\r\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u300cemail\u300d\u9375\u51fa\u73fe\u4e86\u5169\u6b21\uff0c\u9019\u9055\u53cd\u4e86\u9375\u7684\u552f\u4e00\u6027\u7d04\u675f\u3002\u8981\u4fee\u5fa9\u6b64\u932f\u8aa4\uff0c\u60a8\u53ef\u4ee5\u522a\u9664\u5176\u4e2d\u4e00\u500b\u300cemail\u300d\u9375\u6216\u5c07\u5b83\u5011\u91cd\u547d\u540d\u4ee5\u78ba\u4fdd\u552f\u4e00\u6027\uff1a<\/p>\n<pre><code>{\r\n  \"name\": \"John Doe\",\r\n  \"age\": 30,\r\n  \"primary_email\": \"john@example.com\",\r\n  \"secondary_email\": \"johndoe@example.com\"\r\n}<\/code><\/pre>\n<p>\u901a\u904e\u78ba\u4fdd JSON \u5c0d\u8c61\u5167\u7684\u9375\u552f\u4e00\uff0c\u60a8\u53ef\u4ee5\u907f\u514d PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a22037 &#8211; non_unique_keys_in_a_json_object \u7684\u767c\u751f\u3002<\/p>\n<h2 id=\"summary-cNWbrYtTmN\">\u7e3d\u7d50<\/h2>\n<p>\u7e3d\u4e4b\uff0cPostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a22037 &#8211; non_unique_keys_in_a_json_object \u767c\u751f\u5728\u60a8\u5617\u8a66\u63d2\u5165\u6216\u66f4\u65b0\u4e00\u500b\u542b\u6709\u91cd\u8907\u9375\u7684 JSON \u5c0d\u8c61\u6642\u3002\u8981\u4fee\u5fa9\u6b64\u932f\u8aa4\uff0c\u60a8\u9700\u8981\u8b58\u5225\u5f15\u767c\u554f\u984c\u7684 JSON \u5c0d\u8c61\uff0c\u6aa2\u67e5\u91cd\u8907\u9375\uff0c\u5fc5\u8981\u6642\u4f7f\u7528\u9663\u5217\u6216\u5d4c\u5957\u5c0d\u8c61\uff0c\u4e26\u76f8\u61c9\u5730\u66f4\u65b0\u67e5\u8a62\u6216\u64cd\u4f5c\u3002\u901a\u904e\u9019\u4e9b\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u89e3\u6c7a\u8a72\u932f\u8aa4\uff0c\u4e26\u78ba\u4fdd\u60a8\u7684 PostgreSQL \u8cc7\u6599\u5eab\u6b63\u5e38\u904b\u884c\u3002<\/p>\n<p>\u5982\u9700\u53ef\u9760\u4e14\u9ad8\u6548\u7684 VPS \u8a17\u7ba1\u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8003\u616e <a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002\u6211\u5011\u63d0\u4f9b\u9ad8\u6027\u80fd\u4e14\u5b89\u5168\u7684\u8a17\u7ba1\u670d\u52d9\uff0c\u70ba\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\u63d0\u4f9b\u6700\u4f73\u9ad4\u9a57\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-21332","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21332","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=21332"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21332\/revisions"}],"predecessor-version":[{"id":24176,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21332\/revisions\/24176"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}