The `pq.PGError` class appears to be deprecated and it's now suggested <https://godoc.org/github.com/lib/pq#hdr-Errors>to use `pq.Error`. Here's a more concrete example of an insert failing due to a constraint violation (on conflict clause...)
if err, ok := err.(*pq.Error); ok && err.Code.Class().Name() == "integrity_constraint_violation" { ... } On Saturday, June 8, 2013 at 3:37:29 PM UTC-4, Jochen Voss wrote: > > Dear all, > > In a go program, I need to recognise when an insert into a SQL table fails > because the primary key is already present (as opposed to failing for some > other reason). Currently I am checking whether the string value of the > error returned by the Exec method of sql.DB equals "column ... is not > unique". > > My questions: > - Is there a better way of checking for failure due to duplicate keys? > - Is the error string guaranteed to be independent of the > database backend? > > Many thanks, > Jochen > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.