{"id":20829,"date":"2024-01-02T06:38:26","date_gmt":"2024-01-02T06:38:26","guid":{"rendered":"https:\/\/server.hk\/blog\/20829\/"},"modified":"2024-11-14T10:37:49","modified_gmt":"2024-11-14T02:37:49","slug":"how-to-fix-postgresql-error-code-21000-cardinality_violation","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/20829\/","title":{"rendered":"\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a21000 &#8211; cardinality_violation"},"content":{"rendered":"<h1 id=\"how-to-fix-postgresql-error-code-21000-cardinality_violation-lubCTVDMLV\">\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a21000 &#8211; cardinality_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\u3002\u7136\u800c\uff0c\u50cf\u6240\u6709\u8edf\u4ef6\u4e00\u6a23\uff0c\u5b83\u53ef\u80fd\u6703\u9047\u5230\u6703\u5f71\u97ff\u5176\u6b63\u5e38\u904b\u884c\u7684\u932f\u8aa4\u3002\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a21000 &#8211; cardinality_violation\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u6b64\u932f\u8aa4\u7684\u542b\u7fa9\u548c\u4fee\u5fa9\u65b9\u6cd5\u3002<\/p>\n<h2 id=\"understanding-postgresql-error-code-21000-cardinality_violation-lubCTVDMLV\">\u4e86\u89e3 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a21000 &#8211; cardinality_violation<\/h2>\n<p>\u7576\u60a8\u9047\u5230 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a21000 &#8211; cardinality_violation \u6642\uff0c\u610f\u5473\u8457\u60a8\u6b63\u5728\u57f7\u884c\u4e00\u500b\u9055\u53cd\u8868\u7684\u57fa\u6578\u7d04\u675f\u7684\u64cd\u4f5c\u3002\u57fa\u6578\u662f\u6307\u5217\u6216\u7d22\u5f15\u4e2d\u4e0d\u540c\u503c\u7684\u6578\u91cf\u3002\u7576\u60a8\u8a66\u5716\u63d2\u5165\u6216\u66f4\u65b0\u9055\u53cd\u7279\u5b9a\u5217\u7684\u57fa\u6578\u7d04\u675f\u7684\u6578\u64da\u6642\uff0c\u5c31\u6703\u51fa\u73fe\u6b64\u932f\u8aa4\u3002<\/p>\n<p>\u8209\u4f8b\u4f86\u8aaa\uff0c\u5047\u8a2d\u60a8\u6709\u4e00\u500b\u540d\u70ba\u201cusers\u201d\u7684\u8868\uff0c\u5176\u4e2d\u6709\u4e00\u500b\u540d\u70ba\u201cemail\u201d\u7684\u5217\uff0c\u5176\u6709\u4e00\u500b\u552f\u4e00\u6027\u57fa\u6578\u7d04\u675f\u3002\u5982\u679c\u60a8\u5617\u8a66\u63d2\u5165\u4e00\u500b\u5305\u542b\u5728\u8a72\u8868\u4e2d\u5df2\u5b58\u5728\u7684\u96fb\u5b50\u90f5\u4ef6\u5730\u5740\u7684\u65b0\u884c\uff0c\u5c31\u6703\u9047\u5230\u57fa\u6578\u9055\u53cd\u932f\u8aa4\u3002<\/p>\n<h2 id=\"fixing-postgresql-error-code-21000-cardinality_violation-lubCTVDMLV\">\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a21000 &#8211; cardinality_violation<\/h2>\n<p>\u8981\u4fee\u5fa9\u57fa\u6578\u9055\u53cd\u932f\u8aa4\uff0c\u60a8\u9700\u8981\u78ba\u5b9a\u9020\u6210\u9055\u53cd\u7684\u5177\u9ad4\u64cd\u4f5c\u548c\u5217\u3002\u4ee5\u4e0b\u662f\u60a8\u53ef\u4ee5\u9075\u5faa\u7684\u4e00\u4e9b\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-identify-the-query-or-operation-lubCTVDMLV\">1. \u78ba\u8a8d\u67e5\u8a62\u6216\u64cd\u4f5c<\/h3>\n<p>\u9996\u5148\uff0c\u78ba\u5b9a\u89f8\u767c\u932f\u8aa4\u7684\u67e5\u8a62\u6216\u64cd\u4f5c\u3002\u9019\u53ef\u80fd\u662f\u4e00\u689dINSERT\u3001UPDATE\u6216DELETE\u8a9e\u53e5\u3002\u67e5\u627e\u932f\u8aa4\u6d88\u606f\u4e2d\u63d0\u53ca\u7684\u8868\u548c\u5217\u7684\u4efb\u4f55\u5f15\u7528\u3002<\/p>\n<h3 id=\"2-check-cardinality-constraints-lubCTVDMLV\">2. \u6aa2\u67e5\u57fa\u6578\u7d04\u675f<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u67e5\u770b\u8207\u932f\u8aa4\u6709\u95dc\u7684\u8868\u548c\u5217\u7684\u57fa\u6578\u7d04\u675f\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0bSQL\u67e5\u8a62\u6aa2\u7d22\u9019\u4e9b\u4fe1\u606f\uff1a<\/p>\n<pre><code>SELECT conname, conrelid::regclass, a.attname\r\nFROM pg_constraint c\r\nJOIN pg_attribute a ON a.attnum = ANY(c.conkey) AND a.attrelid = c.conrelid\r\nWHERE contype = 'u' AND conrelid = 'your_table'::regclass;\r\n<\/code><\/pre>\n<p>\u5c07\u201cyour_table\u201d\u66ff\u63db\u70ba\u5be6\u969b\u5c0e\u81f4\u932f\u8aa4\u7684\u8868\u7684\u540d\u7a31\u3002\u6b64\u67e5\u8a62\u5c07\u8fd4\u56de\u7d04\u675f\u7684\u540d\u7a31\u3001\u8868\u540d\u548c\u8207\u552f\u4e00\u7d04\u675f\u76f8\u95dc\u7684\u5217\u540d\u3002<\/p>\n<h3 id=\"3-resolve-the-violation-lubCTVDMLV\">3. \u89e3\u6c7a\u9055\u53cd\u554f\u984c<\/h3>\n<p>\u4e00\u65e6\u60a8\u78ba\u5b9a\u4e86\u9055\u53cd\u7684\u64cd\u4f5c\u548c\u7d04\u675f\uff0c\u60a8\u53ef\u4ee5\u91c7\u53d6\u9069\u7576\u7684\u63aa\u65bd\u4f86\u89e3\u6c7a\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u53ef\u80fd\u7684\u89e3\u6c7a\u65b9\u6848\uff1a<\/p>\n<ul>\n<li><strong>\u4fee\u6539\u6578\u64da\uff1a<\/strong> \u5982\u679c\u60a8\u6b63\u5728\u63d2\u5165\u6216\u66f4\u65b0\u6578\u64da\uff0c\u8acb\u78ba\u4fdd\u6578\u64da\u4e0d\u9055\u53cd\u57fa\u6578\u7d04\u675f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6b63\u5728\u63d2\u5165\u4e00\u884c\uff0c\u8acb\u78ba\u4fdd\u552f\u4e00\u5217\u7684\u503c\u5728\u8868\u4e2d\u5c1a\u4e0d\u5b58\u5728\u3002<\/li>\n<li><strong>\u522a\u9664\u6216\u4fee\u6539\u7d04\u675f\uff1a<\/strong> \u5982\u679c\u57fa\u6578\u7d04\u675f\u4e0d\u518d\u5fc5\u8981\u6216\u5c0e\u81f4\u554f\u984c\uff0c\u60a8\u53ef\u4ee5\u8003\u616e\u522a\u9664\u6216\u4fee\u6539\u5b83\u3002\u4e0d\u904e\uff0c\u8acb\u8b39\u614e\uff0c\u9019\u53ef\u80fd\u5f71\u97ff\u6578\u64da\u7684\u5b8c\u6574\u6027\u3002<\/li>\n<li><strong>\u4f7f\u7528\u4e0d\u540c\u7684\u5217\uff1a<\/strong> \u5982\u679c\u57fa\u6578\u7d04\u675f\u9020\u6210\u592a\u591a\u885d\u7a81\uff0c\u60a8\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u4e0d\u540c\u7684\u5217\u6216\u5217\u7d44\u4f86\u5be6\u73fe\u552f\u4e00\u6027\u3002<\/li>\n<\/ul>\n<p>\u8acb\u52d9\u5fc5\u5728\u61c9\u7528\u65bc\u751f\u7522\u74b0\u5883\u4e4b\u524d\u5145\u5206\u6e2c\u8a66\u60a8\u7684\u66f4\u6539\u3002<\/p>\n<h2 id=\"summary-lubCTVDMLV\">\u7e3d\u7d50<\/h2>\n<p>\u7e3d\u800c\u8a00\u4e4b\uff0cPostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a21000 &#8211; cardinality_violation \u767c\u751f\u5728\u60a8\u9055\u53cd\u8868\u7684\u57fa\u6578\u7d04\u675f\u6642\u3002\u8981\u4fee\u5fa9\u6b64\u932f\u8aa4\uff0c\u60a8\u9700\u8981\u78ba\u5b9a\u5c0e\u81f4\u9055\u53cd\u7684\u5177\u9ad4\u64cd\u4f5c\u548c\u5217\uff0c\u67e5\u770b\u57fa\u6578\u7d04\u675f\u4e26\u63a1\u53d6\u9069\u7576\u7684\u63aa\u65bd\uff0c\u4f8b\u5982\u4fee\u6539\u6578\u64da\u6216\u7d04\u675f\u3002\u5982\u679c\u60a8\u9700\u8981\u9032\u4e00\u6b65\u4e86\u89e3\u6709\u95dc PostgreSQL \u6216\u4efb\u4f55\u5176\u4ed6 VPS \u8a17\u7ba1\u670d\u52d9\u7684\u4fe1\u606f\uff0c\u8acb\u8003\u616e\u806f\u7e6b <a href=\"https:\/\/server.hk\">Server.HK<\/a> \u5c0b\u6c42\u5c08\u696d\u652f\u6301\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":[4924],"tags":[],"class_list":["post-20829","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/20829","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=20829"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/20829\/revisions"}],"predecessor-version":[{"id":24250,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/20829\/revisions\/24250"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=20829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=20829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=20829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}