{"record":{"id":"2744185c22fe112c","repo":"jackc/pgx","slug":"cannot-encode-v-as-element-of-range","errorCode":null,"errorMessage":"cannot encode %v as element of range","messagePattern":"cannot encode (.+?) as element of range","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pgtype/range_codec.go","lineNumber":118,"sourceCode":"\t\trangeType |= upperUnboundedMask\n\tcase Exclusive:\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"unknown UpperType: %v\", upperType)\n\t}\n\n\tbuf = append(buf, rangeType)\n\n\tif lowerType != Unbounded {\n\t\tif lower == nil {\n\t\t\treturn nil, fmt.Errorf(\"Lower cannot be NULL unless LowerType is Unbounded\")\n\t\t}\n\n\t\tsp := len(buf)\n\t\tbuf = pgio.AppendInt32(buf, -1)\n\n\t\tlowerPlan := plan.m.PlanEncode(plan.rc.ElementType.OID, BinaryFormatCode, lower)\n\t\tif lowerPlan == nil {\n\t\t\treturn nil, fmt.Errorf(\"cannot encode %v as element of range\", lower)\n\t\t}\n\n\t\tbuf, err = lowerPlan.Encode(lower, buf)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to encode %v as element of range: %w\", lower, err)\n\t\t}\n\t\tif buf == nil {\n\t\t\treturn nil, fmt.Errorf(\"Lower cannot be NULL unless LowerType is Unbounded\")\n\t\t}\n\n\t\tpgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))\n\t}\n\n\tif upperType != Unbounded {\n\t\tif upper == nil {\n\t\t\treturn nil, fmt.Errorf(\"Upper cannot be NULL unless UpperType is Unbounded\")\n\t\t}\n","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/jackc/pgx/blob/ec1a0befd22592cffffdeeb0a50311b506372f4c/pgtype/range_codec.go#L100-L136","documentation":"No encode plan could be found for the range's lower bound: the value's Go type cannot be encoded as the range's element type. This is the type-map lookup failing (PlanEncode returned nil), not a data error.","triggerScenarios":"Thrown at pgtype/range_codec.go:118 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Register an encoder for the element type in the TypeMap, or use a Go type pgx supports","Ensure the bound's Go type matches the range element type declared in the database","Wrap custom types with a codec and register it via pgtype.Map"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"ec1a0befd22592cffffdeeb0a50311b506372f4c","analyzedAt":"2026-08-04T22:52:11.263Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}