Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best way to handle duplicate key exception

 
Pete Neu
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

what is the best way to handle a duplicate key exception?

I would like to have the db continue the insert with the good data sets
and only have a log message for the bad ones.

I use postgresql 7.4.2

cheers,
Pete
 
Pratibha Malhotra
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Handle the Exception appropriately (log messages in your case) and continue inserting non-duplicate records to the table.
 
Rajah Nagur
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The *best* way would be to check whether the record exists. If exists then do an update else insert.

There will be no pain of handling the exceptions.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if they are always supposed to be inserts then you should fix your key management. There are usually several key generators available and it may be as simple as changing to comething else.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic