I was getting duplicate primary key error while inserting a row in a table, despite of the primary key being a serial and me not passing any predefined value for it. So I went down to see what was happening with SELECT last_value FROM tablename_id_seq; and got: 1, despite of table already having several rows. Obviously I would get that duplicate error, but why did serial returned that for me. Any suggestions on what can be happening and how to troubleshoot this?

Tables are migrated up together with some rows, prior to me getting hold on them.

  • 2
    Obviously someone did insert rows without using the default value (and providing an explicit value). That's one of the reasons why it's recommended to use identity columns nowadays instead of serial where this kind of user error can be prevented May 14 '20 at 9:53
  • See here and here and here May 14 '20 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.