package sqlite import ( "errors" sqlite3 "modernc.org/sqlite" ) // SQLite extended result codes for the two constraint failures that mean // "that row is already there". The driver reports them numerically and does // not re-export the constants, so they are named here rather than left as // magic numbers at the call site. const ( constraintUnique = 2067 // SQLITE_CONSTRAINT_UNIQUE constraintPrimaryKey = 1555 // SQLITE_CONSTRAINT_PRIMARYKEY ) // isUniqueViolation reports whether err is SQLite complaining about a // duplicate key. func isUniqueViolation(err error) bool { var sqlErr *sqlite3.Error if !errors.As(err, &sqlErr) { return false } switch sqlErr.Code() { case constraintUnique, constraintPrimaryKey: return true default: return false } }