{"record":{"id":"efe4a56e6341451c","repo":"micro/go-micro","slug":"model-postgres-create-index-w","errorCode":null,"errorMessage":"model/postgres: create index: %w","messagePattern":"model/postgres: create index: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"model/postgres/postgres.go","lineNumber":74,"sourceCode":"\t\tif f.IsKey {\n\t\t\tcol += \" PRIMARY KEY\"\n\t\t}\n\t\tcols = append(cols, col)\n\t}\n\n\tquery := fmt.Sprintf(\"CREATE TABLE IF NOT EXISTS %s (%s)\", quoteIdent(schema.Table), strings.Join(cols, \", \"))\n\tif _, err := d.db.Exec(query); err != nil {\n\t\treturn fmt.Errorf(\"model/postgres: create table: %w\", err)\n\t}\n\n\tfor _, f := range schema.Fields {\n\t\tif f.Index && !f.IsKey {\n\t\t\tidx := fmt.Sprintf(\"CREATE INDEX IF NOT EXISTS %s ON %s (%s)\",\n\t\t\t\tquoteIdent(\"idx_\"+schema.Table+\"_\"+f.Column),\n\t\t\t\tquoteIdent(schema.Table),\n\t\t\t\tquoteIdent(f.Column))\n\t\t\tif _, err := d.db.Exec(idx); err != nil {\n\t\t\t\treturn fmt.Errorf(\"model/postgres: create index: %w\", err)\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nil\n}\n\nfunc (d *postgresModel) schema(v interface{}) (*model.Schema, error) {\n\tt := model.ResolveType(v)\n\td.mu.RLock()\n\ts, ok := d.types[t]\n\td.mu.RUnlock()\n\tif !ok {\n\t\treturn nil, model.ErrNotRegistered\n\t}\n\treturn s, nil\n}\n","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/model/postgres/postgres.go#L56-L92","documentation":"During Register, after creating the table, the postgres model creates a non-unique index for every schema field flagged Index (excluding keys) via CREATE INDEX IF NOT EXISTS. If Postgres rejects the index creation, the error is wrapped as \"model/postgres: create index\" with the driver error as the cause.","triggerScenarios":"Registering a schema where an indexed field's column name is invalid/reserved, the table owner lacks CREATE index privilege, the index name exceeds PostgreSQL's 63-character identifier limit, or a conflicting index of a different definition exists under the same name.","commonSituations":"Long table+column names producing idx_ names > 63 chars (Postgres silently truncates or errors in some toolchains), restricted production DB roles, or schema changes where an existing index conflicts.","solutions":["Inspect the wrapped driver error to see which index/column failed.","Shorten the table or column name so idx_<table>_<column> fits within 63 characters.","Grant CREATE privilege on the schema/table to the connecting role.","Drop or rename the conflicting existing index, or manage indexes via migrations instead."],"exampleFix":"// before\nField{Name: \"very_long_column_name_that_pushes_identifier_over_limit\", Index: true}\n\n// after\n// rename column or disable auto index and create it manually\nField{Name: \"long_col\", Index: true}\n-- manual: CREATE INDEX IF NOT EXISTS idx_tbl_long_col ON tbl (long_col);","handlingStrategy":"try-catch","validationCode":"// check identifier length limit (Postgres max 63 bytes) before Register\nfunc indexNameFits(table, column string) bool {\n    return len(\"idx_\"+table+\"_\"+column) <= 63\n}","typeGuard":null,"tryCatchPattern":"if err := store.Register(ctx, &User{}); err != nil {\n    if strings.Contains(err.Error(), \"create index\") {\n        log.Printf(\"index creation failed (check name length/privileges): %v\", err)\n        // optionally continue without secondary indexes\n        return nil\n    }\n    return err\n}","preventionTips":["Keep table+column names short enough that idx_<table>_<column> stays under 63 characters.","Only set Index: true on fields that genuinely need secondary indexes.","Ensure the DB role can create indexes on the schema.","Check for pre-existing conflicting index names before deploying schema changes."],"tags":["postgres","database","index","ddl"],"backgroundTag":"database-migration-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}