PostgreSQL: duplicate key value violates unique constraint on UPDATE command -


when doing update query, got following error message:

error:  duplicate key value violates unique constraint "tablea_pkey" detail:  key (id)=(47470) exists. 

however, our update query not affect primary key. here simplified version:

update tablea set     items = (         select array_to_string(             array(                 select b.value                 tableb b                 b.a_id = b.id                 group b.name             ),             ','         )     )     a.end_at between now() ,  now() - interval '1 day'; 

we ensured primary key sequence synced:

\d tablea_id_seq 

which produces:

    column     |  type   |          value            ---------------+---------+--------------------------  sequence_name | name    | tablea_id_seq  last_value    | bigint  | 50364  start_value   | bigint  | 1  increment_by  | bigint  | 1  max_value     | bigint  | 9223372036854775807  min_value     | bigint  | 1  cache_value   | bigint  | 1  log_cnt       | bigint  | 0  is_cycled     | boolean | f  is_called     | boolean | t 

looking maximum table index:

select max(id) tablea; 

we got lower value:

  max   -------  50363 (1 row) 

have idea on why such behavior? if exclude problematic id, works.

another strange point replacing previous update by:

update tablea set     items = (         select array_to_string(             array(                 select b.value                 tableb b                 b.a_id = b.id                 group b.name             ),             ','         )     ) a.id = 47470; 

it works well. missing something?

edit: triggers

i have no user-defined triggers on table:

select t.tgname, c.relname pg_trigger t join pg_class c on t.tgrelid = c.oid     c.relname = 'tablea'     ,     t.tgisinternal = false ; 

which returns no row.

note: using psql (postgresql) 9.3.4 version.

not sure cause. however, deleting 2 (non vital) records corresponding existing ids (?) solved issue.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -