{"record":{"id":"d05bd36bfa8dc90d","repo":"micro/go-micro","slug":"errduplicatekey","errorCode":"ErrDuplicateKey","errorMessage":"duplicate key","messagePattern":"duplicate key","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"model/model.go","lineNumber":13,"sourceCode":"// Package model is an interface for structured data storage with schema awareness.\npackage model\n\nimport (\n\t\"context\"\n\t\"errors\"\n)\n\nvar (\n\t// ErrNotFound is returned when a record doesn't exist.\n\tErrNotFound = errors.New(\"not found\")\n\t// ErrDuplicateKey is returned when a record with the same key already exists.\n\tErrDuplicateKey = errors.New(\"duplicate key\")\n\t// ErrNotRegistered is returned when a table has not been registered.\n\tErrNotRegistered = errors.New(\"table not registered\")\n\t// DefaultModel is the default model.\n\tDefaultModel Model = NewModel()\n)\n\n// Model is a structured data storage interface.\ntype Model interface {\n\t// Init initializes the model.\n\tInit(...Option) error\n\t// Register registers a struct type as a table.\n\tRegister(v interface{}, opts ...RegisterOption) error\n\t// Create inserts a new record. Returns ErrDuplicateKey if key exists.\n\tCreate(ctx context.Context, v interface{}) error\n\t// Read retrieves a record by key into v. Returns ErrNotFound if missing.\n\tRead(ctx context.Context, key string, v interface{}) error\n\t// Update modifies an existing record. Returns ErrNotFound if missing.\n\tUpdate(ctx context.Context, v interface{}) error","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/model/model.go#L1-L31","documentation":"model.ErrDuplicateKey is returned when creating a record whose primary key already exists in the store. The model package enforces key uniqueness on Create and surfaces this sentinel so callers can detect conflicts.","triggerScenarios":"Calling Create twice with the same service name/key without deleting the first; two nodes racing to register the same key concurrently; re-running an idempotent-by-intent registration that isn't actually idempotent.","commonSituations":"Restart loops re-registering services; leader-election code where two instances briefly both act as leader; test suites not cleaning the store between runs.","solutions":["Check errors.Is(err, model.ErrDuplicateKey) and treat it as upsert/already-exists where appropriate.","Delete the existing record first or use an update/replace path instead of Create.","Add a unique client/node ID (node.Id) to the key so concurrent registrars don't collide.","Clean the store between test runs to avoid leftovers triggering duplicates."],"exampleFix":"// before\nerr := m.Create(\"services\", rec) // fails if key exists\n// after\nerr := m.Create(\"services\", rec)\nif errors.Is(err, model.ErrDuplicateKey) {\n    err = m.Update(\"services\", rec)\n}","handlingStrategy":"try-catch","validationCode":"null","typeGuard":"func isDuplicate(err error) bool { return errors.Is(err, model.ErrDuplicateKey) }","tryCatchPattern":"err := m.Create(\"services\", rec)\nif errors.Is(err, model.ErrDuplicateKey) {\n    return m.Update(\"services\", rec) // upsert semantics\n}\nreturn err","preventionTips":["Include a unique node/client ID in record keys","Check existence (or delete) before Create when idempotency matters","Clean the store between test runs","Serialize registration under a leader/lock when multiple instances may race"],"tags":["duplicate-key","data-access","sentinel-error","conflict"],"backgroundTag":"duplicate-key-conflict","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}