{"id":21305,"date":"2024-01-02T07:10:26","date_gmt":"2024-01-02T07:10:26","guid":{"rendered":"https:\/\/server.hk\/blog\/21305\/"},"modified":"2024-11-14T10:37:09","modified_gmt":"2024-11-14T02:37:09","slug":"how-to-fix-postgresql-error-code-22004-null_value_not_allowed","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21305\/","title":{"rendered":"\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a22004 &#8211; null_value_not_allowed"},"content":{"rendered":"<h1 id=\"how-to-fix-postgresql-error-code-22004-null_value_not_allowed-TqtFJtdFXD\">\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a22004 &#8211; null_value_not_allowed<\/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\u88ab\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u3002\u7136\u800c\uff0c\u8207\u5176\u4ed6\u8edf\u9ad4\u4e00\u6a23\uff0c\u5b83\u53ef\u80fd\u6703\u9047\u5230\u9700\u8981\u89e3\u6c7a\u7684\u932f\u8aa4\u3002PostgreSQL \u7528\u6236\u53ef\u80fd\u6703\u9047\u5230\u7684\u4e00\u500b\u5e38\u898b\u932f\u8aa4\u662f&#8221;null_value_not_allowed&#8221;\u932f\u8aa4\uff0c\u5176\u932f\u8aa4\u4ee3\u78bc\u70ba22004\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u8a0e\u6b64\u932f\u8aa4\u7684\u539f\u56e0\u4e26\u63d0\u4f9b\u9010\u6b65\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h2 id=\"understanding-the-error-TqtFJtdFXD\">\u4e86\u89e3\u932f\u8aa4<\/h2>\n<p>\u7576\u60a8\u8a66\u5716\u63d2\u5165\u6216\u66f4\u65b0\u4e0d\u5141\u8a31\u4f7f\u7528\u7a7a\u503c\u7684\u6b04\u4f4d\u6642\uff0c\u5c31\u6703\u51fa\u73fe&#8221;null_value_not_allowed&#8221;\u932f\u8aa4\u3002PostgreSQL \u5f37\u5236\u57f7\u884c\u9650\u5236\u689d\u4ef6\u4ee5\u7dad\u8b77\u6578\u64da\u5b8c\u6574\u6027\uff0c\u9632\u6b62\u7121\u6548\u6216\u4e0d\u4e00\u81f4\u7684\u6578\u64da\u5b58\u5132\u5728\u6578\u64da\u5eab\u4e2d\u3002<\/p>\n<p>\u7576\u9019\u7a2e\u932f\u8aa4\u767c\u751f\u6642\uff0cPostgreSQL \u5c07\u986f\u793a\u985e\u4f3c\u4ee5\u4e0b\u7684\u932f\u8aa4\u8a0a\u606f\uff1a<\/p>\n<p><code>ERROR: null value in column \"column_name\" violates not-null constraint<\/code><\/p>\n<p>\u9019\u500b\u932f\u8aa4\u8a0a\u606f\u8868\u660e\u60a8\u6b63\u5728\u5617\u8a66\u5c07\u7a7a\u503c\u63d2\u5165\u6216\u66f4\u65b0\u70ba\u4e0d\u5141\u8a31\u7a7a\u503c\u7684\u6b04\u4f4d\u3002<\/p>\n<h2 id=\"causes-of-the-error-TqtFJtdFXD\">\u932f\u8aa4\u7684\u53ef\u80fd\u6210\u56e0<\/h2>\n<p>&#8220;null_value_not_allowed&#8221;\u932f\u8aa4\u6709\u5e7e\u500b\u53ef\u80fd\u7684\u6210\u56e0\uff1a<\/p>\n<ul>\n<li><strong>\u7f3a\u5931\u6216\u4e0d\u6b63\u78ba\u7684\u9ed8\u8a8d\u503c\uff1a<\/strong> \u5982\u679c\u67d0\u500b\u6b04\u4f4d\u4e0d\u5141\u8a31\u7a7a\u503c\u4e26\u4e14\u6c92\u6709\u5b9a\u7fa9\u9ed8\u8a8d\u503c\uff0c\u7576\u60a8\u5617\u8a66\u5c07\u7a7a\u503c\u63d2\u5165\u5230\u8a72\u6b04\u4f4d\u6642\uff0cPostgreSQL \u5c07\u62cb\u51fa\u932f\u8aa4\u3002<\/li>\n<li><strong>\u6578\u64da\u985e\u578b\u4e0d\u6b63\u78ba\uff1a<\/strong> \u5982\u679c\u60a8\u5617\u8a66\u63d2\u5165\u7684\u6578\u503c\u7684\u6578\u64da\u985e\u578b\u8207\u6b04\u4f4d\u7684\u6578\u64da\u985e\u578b\u4e0d\u76f8\u5bb9\uff0cPostgreSQL \u5c07\u62cb\u51fa\u932f\u8aa4\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u5617\u8a66\u5c07\u5b57\u4e32\u63d2\u5165\u5230\u6574\u6578\u6b04\u4f4d\u4e2d\uff0c\u5c31\u6703\u5f15\u767c\u932f\u8aa4\u3002<\/li>\n<li><strong>\u5916\u9375\u9650\u5236\u689d\u4ef6\uff1a<\/strong> \u5982\u679c\u8a72\u6b04\u4f4d\u662f\u5916\u9375\u9650\u5236\u689d\u4ef6\u7684\u4e00\u90e8\u5206\uff0c\u4e26\u4e14\u60a8\u5617\u8a66\u63d2\u5165\u6216\u66f4\u65b0\u7684\u503c\u5728\u53c3\u7167\u8868\u4e2d\u4e0d\u5b58\u5728\uff0cPostgreSQL \u5c07\u62cb\u51fa\u932f\u8aa4\u3002<\/li>\n<\/ul>\n<h2 id=\"fixing-the-error-TqtFJtdFXD\">\u4fee\u5fa9\u932f\u8aa4<\/h2>\n<p>\u8981\u4fee\u5fa9&#8221;null_value_not_allowed&#8221;\u932f\u8aa4\uff0c\u60a8\u53ef\u4ee5\u9075\u5faa\u4ee5\u4e0b\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-check-the-column-constraints-TqtFJtdFXD\">1. \u6aa2\u67e5\u6b04\u4f4d\u9650\u5236\u689d\u4ef6<\/h3>\n<p>\u9996\u5148\uff0c\u9a57\u8b49\u5c0e\u81f4\u932f\u8aa4\u7684\u6b04\u4f4d\u6240\u5b9a\u7fa9\u7684\u9650\u5236\u689d\u4ef6\u3002\u4f7f\u7528\u4ee5\u4e0b SQL \u67e5\u8a62\u6aa2\u7d22\u8a72\u8868\u7684\u5b9a\u7fa9\uff1a<\/p>\n<pre><code>SELECT column_name, is_nullable, column_default\r\nFROM information_schema.columns\r\nWHERE table_name = 'your_table_name' AND column_name = 'your_column_name';\r\n<\/code><\/pre>\n<p>\u78ba\u4fdd\u8a72\u6b04\u4f4d\u672a\u88ab\u5b9a\u7fa9\u70ba&#8221;NOT NULL&#8221;\u4e26\u4e14\u6c92\u6709\u9ed8\u8a8d\u503c\u70ba\u7a7a\u3002\u5982\u679c\u6709\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u4fee\u6539\u8a72\u8868\u4ee5\u5141\u8a31\u7a7a\u503c\u6216\u63d0\u4f9b\u9ed8\u8a8d\u503c\u3002<\/p>\n<h3 id=\"2-check-the-data-type-TqtFJtdFXD\">2. \u6aa2\u67e5\u6578\u64da\u985e\u578b<\/h3>\n<p>\u5982\u679c\u8a72\u6b04\u4f4d\u5141\u8a31\u7a7a\u503c\u4e26\u4e14\u6c92\u6709\u9ed8\u8a8d\u503c\uff0c\u8acb\u78ba\u4fdd\u60a8\u8981\u63d2\u5165\u6216\u66f4\u65b0\u7684\u6578\u503c\u7684\u6578\u64da\u985e\u578b\u8207\u8a72\u6b04\u4f4d\u7684\u6578\u64da\u985e\u578b\u76f8\u5bb9\u3002\u5982\u6709\u9700\u8981\uff0c\u5728\u63d2\u5165\u6216\u66f4\u65b0\u8a72\u6b04\u4f4d\u4e4b\u524d\u5c07\u6578\u503c\u8f49\u63db\u70ba\u6b63\u78ba\u7684\u6578\u64da\u985e\u578b\u3002<\/p>\n<h3 id=\"3-verify-foreign-key-constraints-TqtFJtdFXD\">3. \u9a57\u8b49\u5916\u9375\u9650\u5236\u689d\u4ef6<\/h3>\n<p>\u5982\u679c\u8a72\u6b04\u4f4d\u662f\u5916\u9375\u9650\u5236\u689d\u4ef6\u7684\u4e00\u90e8\u5206\uff0c\u8acb\u78ba\u4fdd\u60a8\u5617\u8a66\u63d2\u5165\u6216\u66f4\u65b0\u7684\u503c\u5728\u53c3\u7167\u8868\u4e2d\u5b58\u5728\u3002\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5148\u5c07\u7f3a\u5931\u7684\u503c\u63d2\u5165\u5230\u53c3\u7167\u8868\u4e2d\u3002<\/p>\n<h2 id=\"summary-TqtFJtdFXD\">\u6458\u8981<\/h2>\n<p>\u5728 PostgreSQL \u4e2d\uff0c&#8221;null_value_not_allowed&#8221;\u932f\u8aa4\uff0c\u932f\u8aa4\u4ee3\u78bc\u70ba22004\uff0c\u767c\u751f\u5728\u60a8\u8a66\u5716\u63d2\u5165\u6216\u66f4\u65b0\u4e0d\u5141\u8a31\u4f7f\u7528\u7a7a\u503c\u7684\u6b04\u4f4d\u6642\u3002\u8981\u89e3\u6c7a\u6b64\u932f\u8aa4\uff0c\u60a8\u61c9\u6aa2\u67e5\u6b04\u4f4d\u9650\u5236\u689d\u4ef6\u3001\u9a57\u8b49\u6578\u64da\u985e\u578b\uff0c\u4ee5\u53ca\u78ba\u4fdd\u5916\u9375\u9650\u5236\u689d\u4ef6\u5f97\u5230\u6eff\u8db3\u3002\u9075\u5faa\u9019\u4e9b\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u89e3\u6c7a\u932f\u8aa4\u4e26\u7dad\u8b77 PostgreSQL \u6578\u64da\u5eab\u7684\u5b8c\u6574\u6027\u3002<\/p>\n<p>\u6b32\u77ad\u89e3\u66f4\u591a\u6709\u95dc VPS \u8a17\u7ba1\u89e3\u6c7a\u65b9\u6848\u7684\u8cc7\u8a0a\uff0c\u8acb\u67e5\u770b<a href=\"https:\/\/server.hk\">Server.HK<\/a>\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-21305","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21305","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=21305"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21305\/revisions"}],"predecessor-version":[{"id":24210,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21305\/revisions\/24210"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}