{"record":{"id":"decf4faefcde763b","repo":"micro/go-micro","slug":"couldn-t-insert-record-key","errorCode":null,"errorMessage":"Couldn't insert record ${key}","messagePattern":"Couldn't insert record (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"store/mysql/mysql.go","lineNumber":122,"sourceCode":"\t\treturn records, err\n\t}\n\tif cachedTime.Before(time.Now()) {\n\t\t// record has expired\n\t\tgo func() { _ = s.Delete(key) }()\n\t\treturn records, store.ErrNotFound\n\t}\n\trecord.Expiry = time.Until(cachedTime)\n\trecords = append(records, record)\n\n\treturn records, nil\n}\n\n// Write records.\nfunc (s *sqlStore) Write(r *store.Record, opts ...store.WriteOption) error {\n\ttimeCached := time.Now().Add(r.Expiry)\n\t_, err := s.writePrepare.Exec(r.Key, r.Value, timeCached, r.Value, timeCached)\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"Couldn't insert record \"+r.Key)\n\t}\n\n\treturn nil\n}\n\n// Delete records with keys.\nfunc (s *sqlStore) Delete(key string, opts ...store.DeleteOption) error {\n\tresult, err := s.deletePrepare.Exec(key)\n\tif err != nil {\n\t\treturn err\n\t}\n\t_, err = result.RowsAffected()\n\tif err != nil {\n\t\treturn err\n\t}\n\n\treturn nil\n}","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/store/mysql/mysql.go#L104-L140","documentation":"sqlStore.Write wraps any error from the prepared INSERT ... ON DUPLICATE KEY UPDATE statement with the record key so the developer knows which write failed. It means MySQL rejected the upsert for this specific record, not that the store is misconfigured (that fails earlier in initDB).","triggerScenarios":"store.Write(&store.Record{Key: key, ...}) executes s.writePrepare.Exec(...) and MySQL returns an error: connection lost, value exceeds limits, invalid datetime for expiry, lock wait timeout, or table missing.","commonSituations":"MySQL server restarted or connection dropped mid-write; record value larger than max_allowed_packet; expiry timestamps out of range; deadlock/lock wait timeout under concurrent writes to the same key.","solutions":["Log the wrapped cause to identify the MySQL error code","Check MySQL connectivity/max_allowed_packet if values are large","Validate r.Expiry and r.Value sizes/content before calling Write","Reconnect/retry the write; reconfigure the store if the connection pool died"],"exampleFix":"// before\ns.Write(&store.Record{Key: k, Value: hugeBlob, Expiry: 0})\n// after\nif len(hugeBlob) < 16*1024*1024 {\n    s.Write(&store.Record{Key: k, Value: hugeBlob, Expiry: time.Hour})\n}","handlingStrategy":"try-catch","validationCode":"// validate before Write\nif r.Key == \"\" || len(r.Value) > maxAllowed {\n    return errors.New(\"invalid record for mysql store\")\n}","typeGuard":null,"tryCatchPattern":"if err := st.Write(rec); err != nil {\n    if strings.Contains(err.Error(), \"Couldn't insert record\") {\n        log.Printf(\"insert failed for key %s: %+v\", rec.Key, err)\n        // inspect MySQL error code and retry on lock timeouts\n        return retryWrite(rec)\n    }\n    return err\n}","preventionTips":["Keep record values under max_allowed_packet","Validate expiry timestamps are within MySQL timestamp range","Retry transient MySQL errors (1205 lock wait, 1213 deadlock)","Monitor MySQL connection health to catch dropped sessions"],"tags":["mysql","store","write","sql"],"backgroundTag":"sql-insert-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}