{"id":21345,"date":"2024-01-02T07:50:27","date_gmt":"2024-01-02T07:50:27","guid":{"rendered":"https:\/\/server.hk\/blog\/21345\/"},"modified":"2024-11-14T10:36:19","modified_gmt":"2024-11-14T02:36:19","slug":"how-to-fix-postgresql-error-code-23503-foreign_key_violation","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21345\/","title":{"rendered":"\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a23503 &#8211; foreign_key_violation"},"content":{"rendered":"<h1 id=\"how-to-fix-postgresql-error-code-23503-foreign_key_violation-smgipEhzhC\">\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a23503 &#8211; foreign_key_violation<\/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\u8207\u5176\u4ed6\u8edf\u9ad4\u4e00\u6a23\uff0c\u5b83\u4e5f\u53ef\u80fd\u51fa\u73fe\u932f\u8aa4\u3002\u5176\u4e2d\u4e00\u500b PostgreSQL \u4f7f\u7528\u8005\u53ef\u80fd\u9047\u5230\u7684\u5e38\u898b\u932f\u8aa4\u662f\u932f\u8aa4\u4ee3\u78bc\u70ba 23503 \u7684\u300cforeign_key_violation\u300d\u932f\u8aa4\u3002\u9019\u500b\u932f\u8aa4\u767c\u751f\u5728\u64cd\u4f5c\u4e2d\u9055\u53cd\u5916\u9375\u7d04\u675f\u6642\u3002<\/p>\n<h2 id=\"understanding-foreign-key-constraints-smgipEhzhC\">\u7406\u89e3\u5916\u9375\u7d04\u675f<\/h2>\n<p>\u5728\u89e3\u6c7a\u300cforeign_key_violation\u300d\u932f\u8aa4\u4e4b\u524d\uff0c\u91cd\u8981\u7684\u662f\u8981\u4e86\u89e3\u4f55\u70ba\u5916\u9375\u7d04\u675f\u3002\u5728\u95dc\u806f\u5f0f\u6578\u64da\u5eab\u4e2d\uff0c\u5916\u9375\u662f\u6307\u5411\u53e6\u4e00\u500b\u8868\u7684\u4e3b\u9375\u7684\u5b57\u6bb5\u6216\u4e00\u7d44\u5b57\u6bb5\u3002\u5b83\u5efa\u7acb\u4e86\u5169\u500b\u8868\u4e4b\u9593\u7684\u95dc\u4fc2\uff0c\u78ba\u4fdd\u6578\u64da\u5b8c\u6574\u6027\u4e26\u57f7\u884c\u53c3\u7167\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\u53c3\u7167\u8868\u7684\u4e3b\u9375\u5217\u4e2d\u3002\u5982\u679c\u4e00\u500b\u64cd\u4f5c\u9055\u53cd\u4e86\u9019\u500b\u7d04\u675f\uff0c\u6bd4\u5982\u63d2\u5165\u6216\u66f4\u65b0\u4e00\u884c\u8a18\u9304\u6642\u4f7f\u7528\u4e0d\u5b58\u5728\u7684\u5916\u9375\u503c\uff0cPostgreSQL \u5c31\u6703\u62cb\u51fa\u300cforeign_key_violation\u300d\u932f\u8aa4\u3002<\/p>\n<h2 id=\"identifying-the-cause-of-the-error-smgipEhzhC\">\u78ba\u5b9a\u932f\u8aa4\u539f\u56e0<\/h2>\n<p>\u8981\u4fee\u5fa9\u300cforeign_key_violation\u300d\u932f\u8aa4\uff0c\u60a8\u9996\u5148\u9700\u8981\u78ba\u5b9a\u932f\u8aa4\u539f\u56e0\u3002\u932f\u8aa4\u6d88\u606f\u901a\u5e38\u6703\u63d0\u4f9b\u6709\u95dc\u7279\u5b9a\u7d04\u675f\u548c\u6d89\u53ca\u7684\u8868\u7684\u6709\u7528\u4fe1\u606f\u3002\u901a\u5e38\u6703\u5305\u62ec\u7d04\u675f\u7684\u540d\u7a31\u3001\u8868\u540d\u7a31\u548c\u5c0e\u81f4\u9055\u53cd\u7684\u503c\u3002<\/p>\n<p>\u8209\u4f8b\u4f86\u8aaa\uff0c\u932f\u8aa4\u6d88\u606f\u53ef\u80fd\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><code>ERROR:  insert or update on table \"orders\" violates foreign key constraint \"fk_orders_customer_id\"<br \/>\nDETAIL:  Key (customer_id)=(123) is not present in table \"customers\".<\/code><\/p>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u932f\u8aa4\u767c\u751f\u5728\u300corders\u300d\u8868\u4e2d\u63d2\u5165\u6216\u66f4\u65b0\u4e00\u884c\u8a18\u9304\u6642\uff0c\u9055\u53cd\u4e86\u5916\u9375\u7d04\u675f\u300cfk_orders_customer_id\u300d\uff0c\u56e0\u70ba\u300ccustomer_id\u300d\u5217\u4e2d\u7684\u503c123\u5728\u300ccustomers\u300d\u8868\u4e2d\u4e0d\u5b58\u5728\u3002<\/p>\n<h2 id=\"fixing-the-error-smgipEhzhC\">\u4fee\u5fa9\u932f\u8aa4<\/h2>\n<p>\u4e00\u65e6\u60a8\u78ba\u5b9a\u4e86\u300cforeign_key_violation\u300d\u932f\u8aa4\u7684\u539f\u56e0\uff0c\u5c31\u53ef\u4ee5\u63a1\u53d6\u5fc5\u8981\u7684\u6b65\u9a5f\u4f86\u4fee\u5fa9\u5b83\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u89e3\u6c7a\u65b9\u6848\uff1a<\/p>\n<h3 id=\"1-check-data-consistency-smgipEhzhC\">1. \u6aa2\u67e5\u6578\u64da\u4e00\u81f4\u6027<\/h3>\n<p>\u78ba\u4fdd\u5916\u9375\u5217\u4e2d\u7684\u6578\u64da\u8207\u88ab\u53c3\u7167\u8868\u7684\u4e3b\u9375\u5217\u4e2d\u7684\u503c\u76f8\u7b26\u3002\u5982\u679c\u5b58\u5728\u4e0d\u4e00\u81f4\u6027\uff0c\u60a8\u9700\u8981\u76f8\u61c9\u5730\u66f4\u65b0\u6216\u522a\u9664\u9019\u4e9b\u884c\u4ee5\u7dad\u8b77\u6578\u64da\u5b8c\u6574\u6027\u3002<\/p>\n<h3 id=\"2-disable-or-drop-constraints-smgipEhzhC\">2. \u7981\u7528\u6216\u522a\u9664\u7d04\u675f<\/h3>\n<p>\u5982\u679c\u9700\u8981\u66ab\u6642\u57f7\u884c\u9055\u53cd\u5916\u9375\u7d04\u675f\u7684\u64cd\u4f5c\uff0c\u60a8\u53ef\u4ee5\u7981\u7528\u6216\u522a\u9664\u7d04\u675f\u3002\u4e0d\u904e\uff0c\u5728\u57f7\u884c\u6b64\u64cd\u4f5c\u6642\u61c9\u8b39\u614e\uff0c\u56e0\u70ba\u9019\u53ef\u80fd\u5c0e\u81f4\u6578\u64da\u5b8c\u6574\u6027\u554f\u984c\u3002\u64cd\u4f5c\u5b8c\u6210\u5f8c\uff0c\u91cd\u65b0\u555f\u7528\u6216\u91cd\u65b0\u5275\u5efa\u7d04\u675f\u3002<\/p>\n<h3 id=\"3-use-cascade-or-set-null-actions-smgipEhzhC\">3. \u4f7f\u7528 CASCADE \u6216 SET NULL \u64cd\u4f5c<\/h3>\n<p>\u5982\u679c\u5e0c\u671b PostgreSQL \u5728\u53c3\u7167\u884c\u66f4\u65b0\u6216\u522a\u9664\u6642\u81ea\u52d5\u66f4\u65b0\u6216\u522a\u9664\u76f8\u95dc\u884c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 CASCADE \u6216 SET NULL \u64cd\u4f5c\u5b9a\u7fa9\u5916\u9375\u7d04\u675f\u3002\u9019\u78ba\u4fdd\u4e86\u6578\u64da\u7684\u5b8c\u6574\u6027\uff0c\u4e26\u4e14\u4e0d\u6703\u89f8\u767c\u300cforeign_key_violation\u300d\u932f\u8aa4\u3002<\/p>\n<h3 id=\"4-verify-table-order-during-creation-smgipEhzhC\">4. \u5728\u5275\u5efa\u6642\u9a57\u8b49\u8868\u683c\u9806\u5e8f<\/h3>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5275\u5efa\u5e36\u6709\u5916\u9375\u7d04\u675f\u7684\u8868\u683c\uff0c\u8acb\u78ba\u4fdd\u5728\u61c9\u7528\u5916\u9375\u7d04\u675f\u4e4b\u524d\u5275\u5efa\u88ab\u53c3\u7167\u7684\u8868\u683c\u3002\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u5728\u61c9\u7528\u7d04\u675f\u4e4b\u524d\u88ab\u53c3\u7167\u7684\u8868\u683c\u5df2\u7d93\u5b58\u5728\u3002<\/p>\n<h2 id=\"summary-smgipEhzhC\">\u7e3d\u7d50<\/h2>\n<p>\u932f\u8aa4\u4ee3\u78bc\u70ba 23503 \u7684\u300cforeign_key_violation\u300d\u932f\u8aa4\u662f PostgreSQL \u4e2d\u4e00\u500b\u5e38\u898b\u554f\u984c\uff0c\u7576\u9055\u53cd\u5916\u9375\u7d04\u675f\u6642\u5c31\u6703\u51fa\u73fe\u3002\u8981\u4fee\u5fa9\u6b64\u932f\u8aa4\uff0c\u60a8\u9700\u8981\u78ba\u5b9a\u51fa\u73fe\u932f\u8aa4\u7684\u539f\u56e0\uff0c\u901a\u5e38\u53ef\u4ee5\u5728\u932f\u8aa4\u6d88\u606f\u4e2d\u627e\u5230\u3002\u4e00\u65e6\u78ba\u5b9a\uff0c\u60a8\u53ef\u4ee5\u63a1\u53d6\u9069\u7576\u7684\u63aa\u65bd\uff0c\u5982\u6aa2\u67e5\u6578\u64da\u4e00\u81f4\u6027\u3001\u7981\u7528\u6216\u522a\u9664\u7d04\u675f\u3001\u4f7f\u7528 CASCADE \u6216 SET NULL \u64cd\u4f5c\uff0c\u6216\u5728\u5275\u5efa\u6642\u9a57\u8b49\u8868\u683c\u9806\u5e8f\u3002<\/p>\n<p>\u6b32\u77ad\u89e3\u66f4\u591a\u6709\u95dc VPS \u8a17\u7ba1\u89e3\u6c7a\u65b9\u6848\u7684\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f <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-21345","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21345","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=21345"}],"version-history":[{"count":2,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21345\/revisions"}],"predecessor-version":[{"id":24156,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21345\/revisions\/24156"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}