{"id":"76140289af849143","repo":"jackc/pgx","slug":"tried-to-encode-v-via-encoding-to-text-and-scanni","errorCode":null,"errorMessage":"tried to encode %v via encoding to text and scanning but failed due to receiving same type back","messagePattern":"tried to encode (.+?) via encoding to text and scanning but failed due to receiving same type back","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pgtype/pgtype.go","lineNumber":1333,"sourceCode":"\tnewBuf, err = plan.m.Encode(plan.oid, plan.formatCode, v, buf)\n\tif err == nil {\n\t\treturn newBuf, nil\n\t}\n\n\ts, ok := v.(string)\n\tif !ok {\n\t\treturn nil, err\n\t}\n\n\tvar scannedValue any\n\tscanErr := plan.m.Scan(plan.oid, TextFormatCode, []byte(s), &scannedValue)\n\tif scanErr != nil {\n\t\treturn nil, err\n\t}\n\n\t// Prevent infinite loop. We can't encode this. See https://github.com/jackc/pgx/issues/1331.\n\tif reflect.TypeOf(value) == reflect.TypeOf(scannedValue) {\n\t\treturn nil, fmt.Errorf(\"tried to encode %v via encoding to text and scanning but failed due to receiving same type back\", value)\n\t}\n\n\tvar err2 error\n\tnewBuf, err2 = plan.m.Encode(plan.oid, BinaryFormatCode, scannedValue, buf)\n\tif err2 != nil {\n\t\treturn nil, err\n\t}\n\n\treturn newBuf, nil\n}\n\n// TryWrapEncodePlanFunc is a function that tries to create a wrapper plan for value. If successful it returns a plan\n// that will convert the value passed to Encode and then call the next plan. nextValue is value as it will be converted\n// by plan. It must be used to find another suitable EncodePlan. When it is found SetNext must be called on plan for it\n// to be usabled. ok indicates if a suitable wrapper was found.\ntype TryWrapEncodePlanFunc func(value any) (plan WrappedEncodePlanNextSetter, nextValue any, ok bool)\n\ntype derefPointerEncodePlan struct {","sourceCodeStart":1315,"sourceCodeEnd":1351,"githubUrl":"https://github.com/jackc/pgx/blob/ec1a0befd22592cffffdeeb0a50311b506372f4c/pgtype/pgtype.go#L1315-L1351","documentation":"Error \"tried to encode %v via encoding to text and scanning but failed due to receiving same type back\" thrown in jackc/pgx.","triggerScenarios":"Thrown at pgtype/pgtype.go:1333 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"analyzedSha":"ec1a0befd22592cffffdeeb0a50311b506372f4c","analyzedAt":"2026-08-04T22:52:11.263Z","schemaVersion":2}