{"id":17,"date":"2008-05-23T17:31:29","date_gmt":"2008-05-23T16:31:29","guid":{"rendered":"https:\/\/lengers.com\/?p=17"},"modified":"2015-02-01T23:40:05","modified_gmt":"2015-02-01T22:40:05","slug":"posgresqluniqueerror","status":"publish","type":"post","link":"https:\/\/www.lengers.com\/?p=17","title":{"rendered":"PostgreSQL: &#8220;ERROR:  duplicate key violates unique constraint &#8220;reference_pkey&#8221;&#8221;"},"content":{"rendered":"<p>I have been playing with POSTGRESQL and Java a bit and ran into the following problem, and I asked the following on the postgresql mailing list:<br \/>&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>I have the follow SQL statement:<\/p>\n<p>INSERT INTO reference VALUES (DEFAULT,&#8217;123&#8242;,&#8217;2&#8242;,1);<\/p>\n<p>Which generates the following error:<\/p>\n<p>&#8220;ERROR:  duplicate key violates unique constraint &#8220;reference_pkey&#8221;&#8221;<\/p>\n<p>And the table definition looks like:<\/p>\n<p><font face=\"courier new,courier\">CREATE TABLE reference (<br \/>referencelist_nr serial unique,<br \/>reference_text varchar(40) NOT NULL,<br \/>reference_type integer NOT NULL,<br \/>Topic_Id integer NOT NULL,<br \/>PRIMARY KEY (referencelist_nr),<br \/>FOREIGN KEY (Topic_Id) REFERENCES Topic (Topic_Id),<br \/>FOREIGN KEY (reference_type) REFERENCES reference_type (reference_type_nr)<br \/>);<\/font><\/p>\n<p>It seems to me for some reason &#8220;DEFAULT&#8221; doesn&#8217;t select the next SERIAL.<\/p>\n<p>I have been adding data with &#8220;COPY&#8221; into this table&#8230;.does that break something?<br \/>&#8212;&#8212;&#8212;<\/p>\n<p>Of course someone very quickly pointed me to the problem:<\/p>\n<p>I used the &#8220;COPY &#8230; FROM&#8221; command to populate the tables, &#8220;manually&#8221; inserting values into the TABLES.<br \/>This breaks the automatic point in the &#8220;SERIAL&#8221;, which then no longer functions.<\/p>\n<p>The trick was to leave the SERIAL out of the file so POSTGRESQL populated the SERIAL itself, and thereby keeping the pointer intact:<\/p>\n<p>So the copy command will look something like this:<\/p>\n<p><font face=\"courier new,courier\">COPY reference (reference_text,reference_type,topic_id) FROM &#8216;\/zfsdata\/postgreshome\/ref2.txt&#8217;;<\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been playing with POSTGRESQL and Java a bit and ran into the following problem, and I asked the following on the postgresql mailing list:&#8212;&#8212;&#8212;&#8212;&#8211;I have the follow SQL statement: INSERT INTO reference VALUES (DEFAULT,&#8217;123&#8242;,&#8217;2&#8242;,1); Which generates the following error: &#8220;ERROR: duplicate key violates unique constraint &#8220;reference_pkey&#8221;&#8221; And the table definition looks like: CREATE [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","footnotes":""},"categories":[55],"tags":[],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-computing","entry"],"featured_image_src_large":false,"author_info":{"display_name":"Remco","author_link":"https:\/\/www.lengers.com\/?author=1"},"comment_info":0,"category_info":[{"term_id":55,"name":"IT","slug":"computing","term_group":0,"term_taxonomy_id":55,"taxonomy":"category","description":"","parent":0,"count":11,"filter":"raw","cat_ID":55,"category_count":11,"category_description":"","cat_name":"IT","category_nicename":"computing","category_parent":0}],"tag_info":false,"taxonomy_info":{"category":[{"value":55,"label":"IT"}]},"_links":{"self":[{"href":"https:\/\/www.lengers.com\/index.php?rest_route=\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lengers.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lengers.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lengers.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lengers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17"}],"version-history":[{"count":1,"href":"https:\/\/www.lengers.com\/index.php?rest_route=\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":247,"href":"https:\/\/www.lengers.com\/index.php?rest_route=\/wp\/v2\/posts\/17\/revisions\/247"}],"wp:attachment":[{"href":"https:\/\/www.lengers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lengers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lengers.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}