{"record":{"id":"26eb4b12fbadeaa0","repo":"t8y2/dbx","slug":"failed-to-parse-foreign-key-s-referenced-columns","errorCode":null,"errorMessage":"failed to parse foreign key %s referenced columns: %w","messagePattern":"failed to parse foreign key (.+?) referenced columns: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"agents/drivers/kingbase-go/kingbase_metadata.go","lineNumber":1743,"sourceCode":"\tdefer rows.Close()\n\ttype rawForeignKey struct {\n\t\tname, refSchema, refTable string\n\t\tcolumns, refColumns       []int\n\t}\n\trawKeys := []rawForeignKey{}\n\tfor rows.Next() {\n\t\tvar item rawForeignKey\n\t\tvar columnsRaw, refColumnsRaw any\n\t\tif err := rows.Scan(&item.name, &columnsRaw, &refColumnsRaw, &item.refSchema, &item.refTable); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\titem.columns, err = parseCatalogAttributeNumbers(columnsRaw)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to parse foreign key %s columns: %w\", item.name, err)\n\t\t}\n\t\titem.refColumns, err = parseCatalogAttributeNumbers(refColumnsRaw)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to parse foreign key %s referenced columns: %w\", item.name, err)\n\t\t}\n\t\trawKeys = append(rawKeys, item)\n\t}\n\tif err := rows.Err(); err != nil {\n\t\treturn nil, err\n\t}\n\tif len(rawKeys) == 0 {\n\t\treturn []foreignKeyInfo{}, nil\n\t}\n\tlocalAttributes, err := s.relationAttributesByNumber(catalog, prefix, schema, table)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\trefAttributes := map[string]map[int]string{}\n\tresult := []foreignKeyInfo{}\n\tfor _, raw := range rawKeys {\n\t\tkey := raw.refSchema + \"\\x00\" + raw.refTable\n\t\tattributes := refAttributes[key]","sourceCodeStart":1725,"sourceCodeEnd":1761,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/kingbase-go/kingbase_metadata.go#L1725-L1761","documentation":"Returned by listForeignKeysFromCatalog when parseCatalogAttributeNumbers fails to decode c.confkey (the referenced/unique-side column numbers) of a foreign key constraint. Distinguished from the local-columns variant (858) by the \"referenced columns\" wording; it names the constraint and wraps the parse cause. The whole foreign-key listing fails instead of returning a key with unknown referenced columns.","triggerScenarios":"Same legacy V7 catalog path (listForeignKeysFromCatalog) where the scanned c.confkey value is not a comma/space separated integer list — non-standard catalog serialization on old Kingbase versions, corrupted catalog rows, or driver scanning confkey into a type whose fmt.Sprint form isn't parseable.","commonSituations":"Legacy Kingbase V7 servers whose confkey text format differs; constraints referencing tables altered by third-party tools; driver version changes altering the Go representation of the int2[] column.","solutions":["Read the wrapped \"invalid attribute number\" token to identify the unparseable content","Verify confkey directly: SELECT confkey FROM sys_catalog.sys_constraint WHERE conname = '<constraint from message>'","Recreate the foreign key constraint so the catalog regenerates a clean numeric confkey","Upgrade/adjust the driver or compat mode so confkey is delivered in the expected string form"],"exampleFix":"// before: referenced-column parse failure aborts listing\nfks, err := srv.ListForeignKeys(schema, table) // failed to parse foreign key fk_a referenced columns: invalid attribute number \"n\"\n// after: rebuild the constraint\n// ALTER TABLE t DROP CONSTRAINT fk_a; ALTER TABLE t ADD CONSTRAINT fk_a FOREIGN KEY (col) REFERENCES p(id);\nfks, err := srv.ListForeignKeys(schema, table)","handlingStrategy":"try-catch","validationCode":"// Go: pre-check confkey parseability on legacy servers\nrows, _ := db.Query(`SELECT c.conname, c.confkey::text FROM sys_catalog.sys_constraint c WHERE c.contype = 'f'`)\nfor rows.Next() {\n    var name, confkey string\n    rows.Scan(&name, &confkey)\n    for _, tok := range strings.Fields(strings.Trim(confkey, \"{}\")) {\n        if _, err := strconv.Atoi(tok); err != nil {\n            log.Printf(\"FK %s has malformed confkey token %q - recreate constraint\", name, tok)\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"fks, err := srv.ListForeignKeys(schema, table)\nif err != nil {\n    if strings.Contains(err.Error(), \"referenced columns\") {\n        // confkey parse failure: the referenced-side column list is malformed;\n        // drop/re-add the constraint named in the message\n    }\n    return fmt.Errorf(\"list foreign keys: %w\", err)\n}","preventionTips":["Recreate foreign keys via DDL whenever referenced tables are rebuilt or migrated by external tools","Verify confkey text format after Kingbase major-version or driver upgrades","Run metadata smoke tests (indexes + FKs) against legacy V7 instances in CI","Keep the app role's catalog read permissions intact so values are returned untruncated"],"tags":["go","database","kingbase","foreign-key","parsing"],"backgroundTag":"catalog-attribute-parse-failed","analyzedSha":"c0390bff16418b651f4728520d99adf8ce48829a","analyzedAt":"2026-09-05T23:05:10.900Z","contentChangedAt":"2026-09-05T23:05:10.900Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}