{"id":21341,"date":"2024-01-02T07:46:25","date_gmt":"2024-01-02T07:46:25","guid":{"rendered":"https:\/\/server.hk\/blog\/21341\/"},"modified":"2024-11-14T10:36:24","modified_gmt":"2024-11-14T02:36:24","slug":"how-to-fix-postgresql-error-code-2203g-sql_json_item_cannot_be_cast_to_target_type","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21341\/","title":{"rendered":"\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a2203G &#8211; sql_json_item_cannot_be_cast_to_target_type"},"content":{"rendered":"<h1 id=\"how-to-fix-postgresql-error-code-2203g-sql_json_item_cannot_be_cast_to_target_type-FgsqpTYEeD\">\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a2203G &#8211; sql_json_item_cannot_be_cast_to_target_type<\/h1>\n<p>PostgreSQL \u662f\u4e00\u6b3e\u529f\u80fd\u5f37\u5927\u7684\u958b\u6e90\u95dc\u806f\u5f0f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5f0f\u3002\u7136\u800c\uff0c\u50cf\u4efb\u4f55\u8edf\u9ad4\u4e00\u6a23\uff0c\u5b83\u6709\u53ef\u80fd\u906d\u9047\u932f\u8aa4\uff0c\u5e72\u64fe\u5176\u6b63\u5e38\u904b\u4f5c\u3002\u5176\u4e2d\u4e00\u500b\u932f\u8aa4\u662f PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a2203G &#8211; sql_json_item_cannot_be_cast_to_target_type\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u6b64\u932f\u8aa4\u7684\u6210\u56e0\u4e26\u63d0\u4f9b\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h2 id=\"understanding-the-error-FgsqpTYEeD\">\u7406\u89e3\u932f\u8aa4<\/h2>\n<p>PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a2203G &#8211; sql_json_item_cannot_be_cast_to_target_type \u662f\u5728\u9032\u884c\u985e\u578b\u8f49\u63db\u64cd\u4f5c\u6642\uff0cJSON \u6578\u64da\u985e\u578b\u8207\u76ee\u6a19\u6578\u64da\u985e\u578b\u4e4b\u9593\u4e0d\u5339\u914d\u800c\u7522\u751f\u7684\u3002\u7576\u60a8\u8a66\u5716\u5c07\u4e00\u500b JSON \u9805\u76ee\u8f49\u63db\u70ba\u8207\u5176\u5be6\u969b\u503c\u4e0d\u517c\u5bb9\u7684\u76ee\u6a19\u985e\u578b\u6642\uff0c\u901a\u5e38\u6703\u5f15\u767c\u6b64\u932f\u8aa4\u3002<\/p>\n<h2 id=\"possible-causes-FgsqpTYEeD\">\u53ef\u80fd\u7684\u6210\u56e0<\/h2>\n<p>\u6b64\u932f\u8aa4\u53ef\u80fd\u6709\u5e7e\u500b\u6f5b\u5728\u539f\u56e0\uff1a<\/p>\n<ul>\n<li><strong>\u6578\u64da\u985e\u578b\u4e0d\u6b63\u78ba\uff1a<\/strong> \u6307\u5b9a\u7684\u76ee\u6a19\u6578\u64da\u985e\u578b\u8207 JSON \u9805\u76ee\u7684\u5be6\u969b\u6578\u64da\u985e\u578b\u4e0d\u5339\u914d\u3002<\/li>\n<li><strong>\u7121\u6548\u7684 JSON \u503c\uff1a<\/strong> JSON \u9805\u76ee\u5305\u542b\u7121\u6cd5\u8f49\u63db\u70ba\u76ee\u6a19\u6578\u64da\u985e\u578b\u7684\u7121\u6548\u503c\u3002<\/li>\n<li><strong>\u7f3a\u5931\u6216\u4e0d\u6b63\u78ba\u7684 JSON \u8def\u5f91\uff1a<\/strong> \u7528\u4f86\u8a2a\u554f JSON \u6587\u4ef6\u4e2d\u9805\u76ee\u7684 JSON \u8def\u5f91\u7f3a\u5931\u6216\u4e0d\u6b63\u78ba\u3002<\/li>\n<\/ul>\n<h2 id=\"fixing-the-error-FgsqpTYEeD\">\u4fee\u5fa9\u932f\u8aa4<\/h2>\n<p>\u8981\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a2203G &#8211; sql_json_item_cannot_be_cast_to_target_type\uff0c\u60a8\u53ef\u4ee5\u9075\u5faa\u4ee5\u4e0b\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-verify-the-data-types-FgsqpTYEeD\">1. \u9a57\u8b49\u6578\u64da\u985e\u578b<\/h3>\n<p>\u78ba\u4fdd\u6240\u6307\u5b9a\u7684\u76ee\u6a19\u6578\u64da\u985e\u578b\u8207 JSON \u9805\u76ee\u7684\u5be6\u969b\u6578\u64da\u985e\u578b\u76f8\u7b26\u3002\u4f7f\u7528\u9069\u7576\u7684\u985e\u578b\u8f49\u63db\u51fd\u6578\u6216\u904b\u7b97\u7b26\u5c07 JSON \u9805\u76ee\u8f49\u63db\u70ba\u6240\u9700\u7684\u6578\u64da\u985e\u578b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u60f3\u5c07 JSON \u5b57\u7b26\u4e32\u8f49\u63db\u70ba\u6574\u6578\uff0c\u8acb\u4f7f\u7528 <code>::integer<\/code> \u904b\u7b97\u7b26\u3002<\/p>\n<pre><code>SELECT json_column::integer FROM table_name;\r\n<\/code><\/pre>\n<h3 id=\"2-validate-the-json-value-FgsqpTYEeD\">2. \u9a57\u8b49 JSON \u503c<\/h3>\n<p>\u6aa2\u67e5 JSON \u9805\u76ee\uff0c\u78ba\u4fdd\u5176\u4e0d\u5305\u542b\u7121\u6cd5\u8f49\u63db\u70ba\u76ee\u6a19\u6578\u64da\u985e\u578b\u7684\u7121\u6548\u503c\u3002\u4f7f\u7528 <code>json_typeof<\/code> \u51fd\u6578\u4f86\u78ba\u5b9a JSON \u9805\u76ee\u7684\u6578\u64da\u985e\u578b\uff0c\u4e26\u6839\u64da\u60c5\u6cc1\u9032\u884c\u8655\u7406\u3002\u60a8\u4e5f\u53ef\u4ee5\u70ba JSONB \u6578\u64da\u985e\u578b\u4f7f\u7528 <code>jsonb_typeof<\/code> \u51fd\u6578\u3002<\/p>\n<pre><code>SELECT json_typeof(json_column) FROM table_name;\r\n<\/code><\/pre>\n<h3 id=\"3-verify-the-json-path-FgsqpTYEeD\">3. \u9a57\u8b49 JSON \u8def\u5f91<\/h3>\n<p>\u78ba\u4fdd\u7528\u4f86\u8a2a\u554f JSON \u6587\u4ef6\u4e2d\u9805\u76ee\u7684 JSON \u8def\u5f91\u6b63\u78ba\u3002\u4ed4\u7d30\u6aa2\u67e5\u8def\u5f91\u8a9e\u6cd5\uff0c\u4e26\u78ba\u4fdd\u5176\u7b26\u5408 JSON \u6587\u4ef6\u7684\u7d50\u69cb\u3002\u4f7f\u7528 <code>-&gt;<\/code> \u6216 <code>#&gt;<\/code> \u904b\u7b97\u7b26\u4f86\u904d\u6b77 JSON \u7d50\u69cb\u3002<\/p>\n<pre><code>SELECT json_column-&gt;'path' FROM table_name;\r\n<\/code><\/pre>\n<h2 id=\"summary-FgsqpTYEeD\">\u7e3d\u7d50<\/h2>\n<p>\u7e3d\u4e4b\uff0cPostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a2203G &#8211; sql_json_item_cannot_be_cast_to_target_type \u767c\u751f\u5728\u985e\u578b\u8f49\u63db\u64cd\u4f5c\u4e2d\uff0cJSON \u6578\u64da\u985e\u578b\u8207\u76ee\u6a19\u6578\u64da\u985e\u578b\u4e0d\u5339\u914d\u3002\u8981\u4fee\u5fa9\u6b64\u932f\u8aa4\uff0c\u8acb\u9a57\u8b49\u6578\u64da\u985e\u578b\u3001\u6aa2\u67e5 JSON \u503c\u4ee5\u53ca\u9a57\u8b49 JSON \u8def\u5f91\u3002\u5982\u679c\u60a8\u9047\u5230\u5176\u4ed6\u554f\u984c\u6216\u9700\u8981\u6709\u95dc PostgreSQL \u8a17\u7ba1\u7684\u5354\u52a9\uff0c\u8acb\u8003\u616e\u806f\u7e6b <a href=\"https:\/\/server.hk\">Server.HK<\/a>\uff0c\u9019\u662f\u4e00\u5bb6\u63d0\u4f9b\u4e00\u6d41\u670d\u52d9\u7684\u53ef\u9760 VPS \u8a17\u7ba1\u63d0\u4f9b\u5546\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-21341","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21341","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=21341"}],"version-history":[{"count":3,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21341\/revisions"}],"predecessor-version":[{"id":24173,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21341\/revisions\/24173"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}