{"record":{"id":"cdc892080b88ed89","repo":"micro/go-micro","slug":"model-key-field-q-not-set","errorCode":null,"errorMessage":"model: key field %q not set","messagePattern":"model: key field %q not set","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"model/memory.go","lineNumber":64,"sourceCode":"\tt := ResolveType(v)\n\tm.mu.RLock()\n\ts, ok := m.types[t]\n\tm.mu.RUnlock()\n\tif !ok {\n\t\treturn nil, ErrNotRegistered\n\t}\n\treturn s, nil\n}\n\nfunc (m *memoryModel) Create(ctx context.Context, v interface{}) error {\n\tschema, err := m.schema(v)\n\tif err != nil {\n\t\treturn err\n\t}\n\tfields := StructToMap(schema, v)\n\tkey := KeyValue(schema, v)\n\tif key == \"\" {\n\t\treturn fmt.Errorf(\"model: key field %q not set\", schema.Key)\n\t}\n\n\tm.mu.Lock()\n\tdefer m.mu.Unlock()\n\n\ttbl := m.tables[schema.Table]\n\tif _, exists := tbl[key]; exists {\n\t\treturn ErrDuplicateKey\n\t}\n\trow := make(map[string]any, len(fields))\n\tfor k, v := range fields {\n\t\trow[k] = v\n\t}\n\ttbl[key] = row\n\treturn nil\n}\n\nfunc (m *memoryModel) Read(ctx context.Context, key string, v interface{}) error {","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/model/memory.go#L46-L82","documentation":"The in-memory model's Create serializes the struct and extracts the value of the schema's key field to index the record. If that key field is empty (zero value) on the struct instance, the record has no identity to store under, so Create returns this error naming the schema's key field.","triggerScenarios":"Calling memoryModel.Create(ctx, v) with a struct whose key field (schema.Key, e.g. an ID) is the zero value (\"\", 0, nil) and no auto-generation is configured.","commonSituations":"Inserting a new entity without first generating a UUID/ID, constructing records from unmarshaled payloads that omit the ID, or tests reusing structs without resetting/setting keys.","solutions":["Set the key field before calling Create, e.g. v.ID = uuid.NewString().","Configure the model schema to auto-generate keys if the library supports it.","Ensure JSON/unmarshal defaults aren't dropping the ID field (check struct tags and input payload).","Add a validation step in your service layer that rejects entities with empty IDs before persisting."],"exampleFix":"// before\nuser := User{Name: \"alice\"}\nerr := store.Create(ctx, &user)\n\n// after\nuser := User{ID: uuid.NewString(), Name: \"alice\"}\nif user.ID == \"\" {\n    return errors.New(\"id required\")\n}\nerr := store.Create(ctx, &user)","handlingStrategy":"validation","validationCode":"func requireKey(v interface{}, key string) error {\n    rv := reflect.ValueOf(v)\n    if rv.Kind() == reflect.Pointer {\n        rv = rv.Elem()\n    }\n    f := rv.FieldByName(key)\n    if !f.IsValid() || f.IsZero() {\n        return fmt.Errorf(\"key field %q must be set before Create\", key)\n    }\n    return nil\n}\nif err := requireKey(&user, \"ID\"); err != nil {\n    return err\n}\nerr := store.Create(ctx, &user)","typeGuard":null,"tryCatchPattern":"if err := store.Create(ctx, &user); err != nil {\n    if strings.Contains(err.Error(), \"key field\") {\n        return fmt.Errorf(\"%w: generate an ID before Create\", err)\n    }\n    return err\n}","preventionTips":["Generate IDs (uuid.NewString()) in the constructor/New function of your entity type.","Add a domain-level Validate() that checks the key before any persistence call.","Ensure JSON unmarshaling preserves the ID (check json tags and payloads).","Prefer schemas with auto-generated keys if the model layer supports them."],"tags":["model","storage","primary-key","validation"],"backgroundTag":"missing-primary-key","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}