{"record":{"id":"355d7dbc942025cf","repo":"micro/go-micro","slug":"model-postgres-update-w","errorCode":null,"errorMessage":"model/postgres: update: %w","messagePattern":"model/postgres: update: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"model/postgres/postgres.go","lineNumber":141,"sourceCode":"\tmodel.MapToStruct(schema, fields, v)\n\treturn nil\n}\n\nfunc (d *postgresModel) Update(ctx context.Context, v interface{}) error {\n\tschema, err := d.schema(v)\n\tif err != nil {\n\t\treturn err\n\t}\n\tfields := model.StructToMap(schema, v)\n\tkey := model.KeyValue(schema, v)\n\tsetClauses, values := buildUpdate(schema, fields)\n\tvalues = append(values, key)\n\tparamIdx := len(values)\n\tquery := fmt.Sprintf(\"UPDATE %s SET %s WHERE %s = $%d\",\n\t\tquoteIdent(schema.Table), setClauses, quoteIdent(schema.Key), paramIdx)\n\tresult, err := d.db.ExecContext(ctx, query, values...)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"model/postgres: update: %w\", err)\n\t}\n\tn, _ := result.RowsAffected()\n\tif n == 0 {\n\t\treturn model.ErrNotFound\n\t}\n\treturn nil\n}\n\nfunc (d *postgresModel) Delete(ctx context.Context, key string, v interface{}) error {\n\tschema, err := d.schema(v)\n\tif err != nil {\n\t\treturn err\n\t}\n\tquery := fmt.Sprintf(\"DELETE FROM %s WHERE %s = $1\", quoteIdent(schema.Table), quoteIdent(schema.Key))\n\tresult, err := d.db.ExecContext(ctx, query, key)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"model/postgres: delete: %w\", err)\n\t}","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/model/postgres/postgres.go#L123-L159","documentation":"Update builds an UPDATE ... WHERE key = $n statement from the struct's non-key fields and executes it. Any SQL failure is wrapped as \"model/postgres: update\"; additionally, if the statement succeeds but affects zero rows, the method returns model.ErrNotFound, meaning no record matched the key.","triggerScenarios":"Calling postgresModel.Update(ctx, v) where the SQL errors (type mismatch, NOT NULL violation, schema drift, connection issue) — wrapped error; or where the key field of v doesn't match any existing row — ErrNotFound variant.","commonSituations":"Updating a record that was deleted concurrently, stale client caches holding old IDs, PATCH handlers sending structs with wrong types, or mismatched key type between model schema and table column.","solutions":["Verify the record exists (Read by key) before/after Update; handle model.ErrNotFound for stale IDs.","Unwrap the error to see the driver's message and fix the offending column/type.","Ensure the key field is set and matches the stored primary key exactly (type and value).","Run migrations if the table schema drifted from the model definition.","Check connectivity and transaction state if the cause is a connection error."],"exampleFix":"// before\nif err := store.Update(ctx, &user); err != nil {\n    return err\n}\n\n// after\nif err := store.Update(ctx, &user); err != nil {\n    if errors.Is(err, model.ErrNotFound) {\n        return http.StatusNotFound\n    }\n    return fmt.Errorf(\"update user %s: %w\", user.ID, err)\n}","handlingStrategy":"try-catch","validationCode":"// confirm the row exists before Update\nvar exists bool\nif err := db.QueryRow(\n    \"SELECT EXISTS (SELECT 1 FROM users WHERE id = $1)\", user.ID,\n).Scan(&exists); err != nil {\n    return err\n}\nif !exists {\n    return model.ErrNotFound\n}","typeGuard":null,"tryCatchPattern":"if err := store.Update(ctx, &user); err != nil {\n    if errors.Is(err, model.ErrNotFound) {\n        return ErrUserGone // zero rows affected: key doesn't match any row\n    }\n    var pgErr *pgconn.PgError\n    if errors.As(err, &pgErr) {\n        log.Printf(\"update failed [%s]: %s\", pgErr.Code, pgErr.Message)\n    }\n    return fmt.Errorf(\"update: %w\", err)\n}","preventionTips":["Handle model.ErrNotFound (RowsAffected == 0) explicitly for stale/deleted records.","Verify the key field is populated and correctly typed before Update.","Run migrations so the table schema matches the model definition.","Use optimistic concurrency or re-read on conflict when records may be deleted concurrently."],"tags":["postgres","database","update","sql"],"backgroundTag":"sql-update-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}