{"id":21342,"date":"2024-01-02T07:47:25","date_gmt":"2024-01-02T07:47:25","guid":{"rendered":"https:\/\/server.hk\/blog\/21342\/"},"modified":"2024-11-14T10:36:23","modified_gmt":"2024-11-14T02:36:23","slug":"how-to-fix-postgresql-error-code-23000-integrity_constraint_violation","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21342\/","title":{"rendered":"\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a23000 &#8211; integrity_constraint_violation"},"content":{"rendered":"<h1>\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a23000 &#8211; integrity_constraint_violation<\/h1>\n<p>PostgreSQL \u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u958b\u6e90\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u3002\u7136\u800c\uff0c\u548c\u5176\u4ed6\u8edf\u4ef6\u4e00\u6a23\uff0c\u5b83\u4e5f\u4e0d\u514d\u51fa\u73fe\u932f\u8aa4\u3002\u7528\u6236\u53ef\u80fd\u6703\u9047\u5230\u7684\u5e38\u898b\u932f\u8aa4\u4e4b\u4e00\u662f PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a23000 &#8211; integrity_constraint_violation\u3002\u9019\u500b\u932f\u8aa4\u767c\u751f\u5728\u5916\u9375\u7d04\u675f\u88ab\u9055\u53cd\u6642\uff0c\u9019\u610f\u5473\u8457\u4e00\u500b\u8868\u4e2d\u7684\u8a18\u9304\u5f15\u7528\u4e86\u53e6\u4e00\u500b\u8868\u4e2d\u4e0d\u5b58\u5728\u7684\u8a18\u9304\u3002<\/p>\n<h2>\u4e86\u89e3\u5916\u9375\u7d04\u675f<\/h2>\n<p>\u5728\u6211\u5011\u6df1\u5165\u4fee\u5fa9 integrity_constraint_violation \u932f\u8aa4\u4e4b\u524d\uff0c\u9996\u5148\u8981\u4e86\u89e3\u5916\u9375\u7d04\u675f\u662f\u4ec0\u9ebc\u3002\u5728 PostgreSQL \u4e2d\uff0c\u5916\u9375\u662f\u6307\u4e00\u500b\u8868\u4e2d\u5b57\u6bb5\u6216\u5b57\u6bb5\u96c6\uff0c\u8a72\u5b57\u6bb5\u5f15\u7528\u4e86\u53e6\u4e00\u500b\u8868\u7684\u4e3b\u9375\u3002\u5b83\u5efa\u7acb\u4e86\u5169\u500b\u8868\u4e4b\u9593\u7684\u95dc\u4fc2\uff0c\u78ba\u4fdd\u6578\u64da\u5b8c\u6574\u6027\u4e26\u5f37\u5236\u57f7\u884c\u53c3\u8003\u5b8c\u6574\u6027\u898f\u5247\u3002<\/p>\n<p>\u7576\u5b9a\u7fa9\u5916\u9375\u7d04\u675f\u6642\uff0c\u9019\u610f\u5473\u8457\u5916\u9375\u5217\u4e2d\u7684\u503c\u5fc5\u9808\u5b58\u5728\u65bc\u88ab\u5f15\u7528\u8868\u7684\u4e3b\u9375\u5217\u4e2d\u3002\u5982\u679c\u53c3\u8003\u8868\u4e2d\u7684\u8a18\u9304\u5f15\u7528\u4e86\u88ab\u5f15\u7528\u8868\u4e2d\u4e0d\u5b58\u5728\u7684\u8a18\u9304\uff0c\u5247\u6703\u89f8\u767c integrity_constraint_violation \u932f\u8aa4\u3002<\/p>\n<h2>\u8b58\u5225\u932f\u8aa4\u539f\u56e0<\/h2>\n<p>\u7576\u60a8\u9047\u5230 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a23000 &#8211; integrity_constraint_violation \u6642\uff0c\u8b58\u5225\u932f\u8aa4\u7684\u539f\u56e0\u81f3\u95dc\u91cd\u8981\u3002\u60a8\u53ef\u4ee5\u6aa2\u67e5\u932f\u8aa4\u4fe1\u606f\uff0c\u901a\u5e38\u6703\u63d0\u4f9b\u6709\u95dc\u5177\u9ad4\u9055\u53cd\u7684\u7d04\u675f\u548c\u76f8\u95dc\u8868\u7684\u4fe1\u606f\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u932f\u8aa4\u4fe1\u606f\u53ef\u80fd\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><code>ERROR:  insert or update on table \"table_name\" violates foreign key constraint \"constraint_name\" DETAIL:  Key (foreign_key_column)=(value) is not present in table \"referenced_table\".<\/code><\/p>\n<p>\u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0c\u201ctable_name\u201d\u6307\u7684\u662f\u9055\u53cd\u7d04\u675f\u7684\u8868\uff0c\u201cconstraint_name\u201d\u662f\u5916\u9375\u7d04\u675f\u7684\u540d\u7a31\uff0c\u800c\u201creferenced_table\u201d\u5247\u662f\u88ab\u5f15\u7528\u7684\u8868\u3002<\/p>\n<h2>\u4fee\u5fa9 integrity_constraint_violation \u932f\u8aa4<\/h2>\n<p>\u4e00\u65e6\u60a8\u78ba\u5b9a\u4e86\u932f\u8aa4\u7684\u539f\u56e0\uff0c\u53ef\u4ee5\u63a1\u53d6\u5e7e\u500b\u6b65\u9a5f\u4f86\u4fee\u5fa9\u5b83\uff1a<\/p>\n<h3>1. \u6aa2\u67e5\u6578\u64da\u4e00\u81f4\u6027<\/h3>\n<p>\u9996\u5148\uff0c\u78ba\u4fdd\u60a8\u8868\u4e2d\u7684\u6578\u64da\u662f\u4e00\u81f4\u7684\u3002\u9a57\u8b49\u88ab\u5f15\u7528\u7684\u8868\u4e2d\u662f\u5426\u5305\u542b\u5916\u9375\u5728\u53c3\u8003\u8868\u4e2d\u6240\u6307\u7684\u8a18\u9304\u3002\u5982\u679c\u6709\u8a18\u9304\u7f3a\u5931\u6216\u5df2\u88ab\u522a\u9664\uff0c\u60a8\u9700\u8981\u6062\u5fa9\u6216\u91cd\u65b0\u5275\u5efa\u5b83\u5011\u3002<\/p>\n<h3>2. \u66f4\u65b0\u6216\u522a\u9664\u8a18\u9304<\/h3>\n<p>\u5982\u679c\u88ab\u5f15\u7528\u7684\u8868\u5305\u542b\u5fc5\u8981\u7684\u8a18\u9304\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u66f4\u65b0\u6216\u522a\u9664\u53c3\u8003\u8868\u4e2d\u9020\u6210\u9055\u53cd\u7684\u8a18\u9304\u3002\u8acb\u78ba\u4fdd\u4ee5\u7dad\u8b77\u6578\u64da\u5b8c\u6574\u6027\u548c\u7b26\u5408\u61c9\u7528\u908f\u8f2f\u7684\u65b9\u5f0f\u66f4\u65b0\u6216\u522a\u9664\u8a18\u9304\u3002<\/p>\n<h3>3. \u7981\u7528\u6216\u522a\u9664\u7d04\u675f<\/h3>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u66ab\u6642\u7981\u7528\u6216\u522a\u9664\u5916\u9375\u7d04\u675f\u4ee5\u89e3\u6c7a integrity_constraint_violation \u932f\u8aa4\u3002\u7136\u800c\uff0c\u9019\u6a23\u505a\u6642\u8981\u8b39\u614e\uff0c\u56e0\u70ba\u5982\u679c\u8655\u7406\u4e0d\u7576\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4\u6578\u64da\u4e0d\u4e00\u81f4\u3002\u4e00\u65e6\u554f\u984c\u89e3\u6c7a\uff0c\u8acb\u91cd\u65b0\u555f\u7528\u6216\u91cd\u5efa\u7d04\u675f\uff0c\u4ee5\u78ba\u4fdd\u6578\u64da\u5b8c\u6574\u6027\u3002<\/p>\n<h3>4. \u4f7f\u7528 CASCADE \u6216 SET NULL \u64cd\u4f5c<\/h3>\n<p>\u5982\u679c\u60a8\u5e0c\u671b\u5728\u66f4\u65b0\u6216\u522a\u9664\u88ab\u5f15\u7528\u8a18\u9304\u6642\uff0c\u81ea\u52d5\u66f4\u65b0\u6216\u522a\u9664\u53c3\u8003\u8868\u4e2d\u7684\u8a18\u9304\uff0c\u53ef\u4ee5\u4f7f\u7528 CASCADE \u6216 SET NULL \u64cd\u4f5c\u3002\u9019\u4e9b\u64cd\u4f5c\u6709\u52a9\u65bc\u7dad\u8b77\u6578\u64da\u4e00\u81f4\u6027\uff0c\u4e26\u9632\u6b62\u672a\u4f86\u51fa\u73fe integrity_constraint_violation \u932f\u8aa4\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u7e3d\u4e4b\uff0cPostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a23000 &#8211; integrity_constraint_violation \u767c\u751f\u5728\u5916\u9375\u7d04\u675f\u88ab\u9055\u53cd\u6642\u3002\u8981\u4fee\u5fa9\u6b64\u932f\u8aa4\uff0c\u60a8\u9700\u8981\u8b58\u5225\u539f\u56e0\uff0c\u6aa2\u67e5\u6578\u64da\u4e00\u81f4\u6027\uff0c\u5fc5\u8981\u6642\u66f4\u65b0\u6216\u522a\u9664\u8a18\u9304\uff0c\u5982\u679c\u9700\u8981\uff0c\u7981\u7528\u6216\u522a\u9664\u7d04\u675f\uff0c\u4e26\u8003\u616e\u4f7f\u7528 CASCADE \u6216 SET NULL \u64cd\u4f5c\u3002\u5982\u679c\u60a8\u9700\u8981\u53ef\u9760\u4e14\u9ad8\u6548\u80fd\u7684 VPS \u8a17\u7ba1\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528 <a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002\u6211\u5011\u7684\u9999\u6e2f VPS \u8a17\u7ba1\u670d\u52d9\u65e8\u5728\u6eff\u8db3\u60a8\u7684\u5177\u9ad4\u9700\u6c42\uff0c\u4e26\u70ba\u60a8\u7684 PostgreSQL \u6578\u64da\u5eab\u63d0\u4f9b\u5353\u8d8a\u7684\u6027\u80fd\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-21342","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21342","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=21342"}],"version-history":[{"count":2,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21342\/revisions"}],"predecessor-version":[{"id":24162,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21342\/revisions\/24162"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}