Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hO3Mz-0003xq-3Q for pgsql-general@arkaria.postgresql.org; Tue, 07 May 2019 16:55:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hO3Mx-0005IV-MX for pgsql-general@arkaria.postgresql.org; Tue, 07 May 2019 16:55:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hO3Mx-0005HC-95 for pgsql-general@lists.postgresql.org; Tue, 07 May 2019 16:55:07 +0000 Received: from mail-oi1-x241.google.com ([2607:f8b0:4864:20::241]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hO3Mt-0003ww-5X for pgsql-general@lists.postgresql.org; Tue, 07 May 2019 16:55:06 +0000 Received: by mail-oi1-x241.google.com with SMTP id m204so7255030oib.0 for ; Tue, 07 May 2019 09:55:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:subject:from:to:cc:date:in-reply-to:references :mime-version; bh=/bP3PfRTCWVEZAsuZ4X504/f8V+6UuunlpgpLLSzf4I=; b=Bl0oM17YX2FuvWU2ZUrl6JVvPzPGq/81AQ2naKdeLYAkKeT8s9bRAfyQdQCRKafwT8 8fXxUad48fF0LDs6r4RFlNZTIiIGEeiTUwZsO93WHXVNyn4n8/RBx6KF3vFriP3/vSIZ sq0nMFMUy0k0m7OVr4NKgC1wzLWnLzDeFF1IaPaxUPCZSUn4WP0g0TXjBKPzwmVQc3j+ WS9D5xRKnrvG4QF+Uid+Mb+Qifa8Qpf3WafUE3Z+2tjRR9XZxhaI/+NUgUuLsftB/1RN Lr2swKHvRwk8YaM9lkZCnGNvxOwMhW/MWNC9qZfbE6Q8FzNebv+4tW8/fdf4TxV5z+bL VGPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:mime-version; bh=/bP3PfRTCWVEZAsuZ4X504/f8V+6UuunlpgpLLSzf4I=; b=KA8bUmD/FsHO7ipzvglzDYNMwgRX/UKBa3JcgoHfeKGL/6iTVSp4MwrXzcYkaPSE1m 4PeN4xtpqIarK5+WChq3AA24Jids7Ei+cwg5CJRmxddHAsWoKD0RI38FfPl5o0iAD4i3 zKTn88B2xa9AnZv4UTmvNlBs4/WJjHigYc+P/703eA13xiJC2d5uP+z9ebzeR204NVBg BLVhykVmgxg2DG6qDi6hhr/6NrFU25UBHORaoHghfjclVbJ/vk9P6OYSEA+LbIU5MyEa 3grpYx+zvostTLbVj5zyraDxOqV57akRkpMmXEKo1BPb2JbxJgU4KB/SHCahqKaE2Z3X HrDA== X-Gm-Message-State: APjAAAVXLCQJ/+ZIdwcRp5wxwp3ef+bLmP9g8Arcsy+T+7yOIE+Qgf/7 hl33mR6hy/wCthbnOaJmJqo= X-Google-Smtp-Source: APXvYqzy4LxkPA1Fz+POH+5RNZ/W156h7ohutMPaLRoKLIULZKKawJowhz7mzIQHWTmqKgt/TwMvng== X-Received: by 2002:aca:bd09:: with SMTP id n9mr885344oif.56.1557248100819; Tue, 07 May 2019 09:55:00 -0700 (PDT) Received: from frodriguez-HP-Notebook ([189.208.128.209]) by smtp.googlemail.com with ESMTPSA id w131sm6030454oig.29.2019.05.07.09.54.59 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 07 May 2019 09:54:59 -0700 (PDT) Message-ID: Subject: Re: duplicate key value violates unique constraint "chinese_price_infos_pkey" From: Frank Alberto Rodriguez To: Arup Rakshit , Ray O'Donnell Cc: pgsql-general@lists.postgresql.org Date: Tue, 07 May 2019 16:54:57 +0000 In-Reply-To: <1942A78B-B9F6-4FCC-B402-EA361F74B350@zeit.io> References: <1942A78B-B9F6-4FCC-B402-EA361F74B350@zeit.io> Content-Type: multipart/alternative; boundary="=-5Z//4fQyMq5Sa3LPW9i+" X-Mailer: Evolution 3.28.5-0ubuntu0.18.04.1 Mime-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --=-5Z//4fQyMq5Sa3LPW9i+ Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit You can fix the problem with this query: SELECT setval('chinese_price_infos_id_seq', sq.val) from ( SELECT MAX(id) as val FROM chinese_price_infos ) sq; But you have to search in your application because in some point the app are inserting the id column instead of leave this task to the DB. If you are using some entity framework and the app fill the ID field in a new entity in some point of the workflow, then when you save the entity, the framework automatically saves the ID in the DB without checking if the ID already exist. Sheers On Mon, 2019-05-06 at 16:40 +0530, Arup Rakshit wrote: > Hi, > > Thanks for your reply. It is automatic, my app don’t creates ID, it delegates it to the DB. I am using Ruby on Rails app, where we use Postgresql. > > docking_dev=# \d chinese_price_infos; > Table "public.chinese_price_infos" > Column | Type | Collation | Nullable | Default > -------------+-----------------------------+-----------+----------+------------------------------------------------- > id | integer | | not null | nextval('chinese_price_infos_id_seq'::regclass) > created_at | timestamp without time zone | | | > updated_at | timestamp without time zone | | | > item_code | character varying(255) | | | > description | character varying(255) | | | > unit | character varying(255) | | | > price_cents | integer | | | > uuid | uuid | | | uuid_generate_v4() > company_id | uuid | | | > Indexes: > "chinese_price_infos_pkey" PRIMARY KEY, btree (id) > "index_chinese_price_infos_on_company_id" btree (company_id) > > > > > Thanks, > > Arup Rakshit > ar@zeit.io > > > > On 06-May-2019, at 4:38 PM, Ray O'Donnell wrote: > > On 06/05/2019 12:05, Arup Rakshit wrote: > Every time I try to insert I get the error: > docking_dev=# INSERT INTO "chinese_price_infos" ("item_code", > "price_cents", "unit", "description", "company_id", "created_at", > "updated_at") VALUES ('01GS10001', 6000, 'Lift', 'Shore Crane > Rental', '9ae3f8b8-8f3f-491c-918a-efd8f5100a5e', '2019-05-06 > 10:49:03.894725', '2019-05-06 10:49:03.894725'); ERROR: duplicate > key value violates unique constraint "chinese_price_infos_pkey" DETAIL: Key (id)=(71165) already exists. docking_dev=# INSERT INTO > "chinese_price_infos" ("item_code", "price_cents", "unit", > "description", "company_id", "created_at", "updated_at") VALUES > ('01GS10001', 6000, 'Lift', 'Shore Crane Rental', > '9ae3f8b8-8f3f-491c-918a-efd8f5100a5e', '2019-05-06 10:49:03.894725', > '2019-05-06 10:49:03.894725'); ERROR: duplicate key value violates > unique constraint "chinese_price_infos_pkey" DETAIL: Key > (id)=(71166) already exists. > Then I found: > docking_dev=# SELECT MAX(id) FROM chinese_price_infos; max -------- 128520 (1 row) > docking_dev=# SELECT nextval('chinese_price_infos_id_seq'); nextval --------- 71164 (1 row) > Not sure how it is out of sync. How can I fix this permanently. I ran > vacuum analyze verbose; still same error. > > > You can fix it by using setval() to set the sequence manually to something higher than the highest current id value in the table. However, it sounds as if something in the application code may be causing problems.... For example, is something generating id values without reference to the sequence? > > Ray. > > > > -- > Raymond O'Donnell // Galway // Ireland > ray@rodonnell.ie > > > > --=-5Z//4fQyMq5Sa3LPW9i+ Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable
You can fix the problem with this query:

= SELECT setval('chinese_price_infos_id_seq', sq.val) from ( SELECT MAX(id) a= s val FROM chinese_price_infos ) sq;

But you have = to search in your application because in some point the app are inserting t= he id column instead of leave this task to the DB. 
If you a= re using some entity framework and the app fill the ID field in a new entit= y in some point of the workflow, then when you save the entity, the fr= amework automatically saves the ID in the DB without checking if the ID alr= eady exist.


Sheers

On Mon, 2019-05-06 at 16:40 +0530, Arup Rakshit wrote:
Hi,

Thanks for your repl=
y. It is automatic, my app don=E2=80=99t creates ID, it delegates it to the=
 DB. I am using Ruby on Rails app, where we use Postgresql. 

=
docking_dev=3D# \d chinese_price_infos;
              =
                           Table "public.chinese_price_infos"
   =
Column    |            Type             | Collation | Nullable |           =
          Default                     
-------------+------------=
-----------------+-----------+----------+----------------------------------=
---------------
 id          | integer                     |     =
      | not null | nextval('chinese_price_infos_id_seq'::regclass)
created_at | timestamp without time zone | | |
 updated_at  | timestamp without time zone |           |          | <=
/pre>
 item_code   | character varying(255)      |           |         =
 | 
 description | character varying(255)      |           |     =
     | 
 unit        | character varying(255)      |           | =
         | 
 price_cents | integer                     |         =
  |          | 
 uuid        | uuid                        |     =
      |          | uuid_generate_v4()
 company_id  | uuid        =
                |           |          | 
Indexes:
    =
"chinese_price_infos_pkey" PRIMARY KEY, btree (id)
    "index_chi=
nese_price_infos_on_company_id" btree (company_id)




Thanks,

<= pre>Arup Rakshit
ar@zeit.io



On 06-May-2019, at 4:38=
 PM, Ray O'Donnell <ray@rodonnell.ie=
> wrote:

On 06/05/2019 12:05, Arup Rakshit =
wrote:
Every time I try to insert I get the error:
dock=
ing_dev=3D# INSERT INTO "chinese_price_infos" ("item_code",
"pric=
e_cents", "unit", "description", "company_id", "created_at",
"upd=
ated_at") VALUES ('01GS10001', 6000, 'Lift', 'Shore Crane
Rental'=
, '9ae3f8b8-8f3f-491c-918a-efd8f5100a5e', '2019-05-06
10:49:03.89=
4725', '2019-05-06 10:49:03.894725'); ERROR:  duplicate
key value=
 violates unique constraint "chinese_price_infos_pkey" DETAIL:  Key (id)=3D=
(71165) already exists. docking_dev=3D# INSERT INTO
"chinese_pric=
e_infos" ("item_code", "price_cents", "unit",
"description", "com=
pany_id", "created_at", "updated_at") VALUES
('01GS10001', 6000, =
'Lift', 'Shore Crane Rental',
'9ae3f8b8-8f3f-491c-918a-efd8f5100a=
5e', '2019-05-06 10:49:03.894725',
'2019-05-06 10:49:03.894725');=
 ERROR:  duplicate key value violates
unique constraint "chinese_=
price_infos_pkey" DETAIL:  Key
(id)=3D(71166) already exists.
Then I found:
docking_dev=3D# SELECT MAX(id) FROM chinese_=
price_infos; max -------- 128520 (1 row)
docking_dev=3D# SELECT n=
extval('chinese_price_infos_id_seq'); nextval --------- 71164 (1 row)
=
Not sure how it is out of sync. How can I fix this permanently. I ran<=
/pre>
vacuum analyze verbose; still same error.


You can fix it by using setval() to set the sequence manua=
lly to something higher than the highest current id value in the table. How=
ever, it sounds as if something in the application code may be causing prob=
lems.... For example, is something generating id values without reference t=
o the sequence?

Ray.


=

-- 
Raymond O'Donnell // Galway // Irel=
and
ray@rodonnell.ie




<= /body> --=-5Z//4fQyMq5Sa3LPW9i+--