{"record":{"id":"6829244272532db0","repo":"t8y2/dbx","slug":"failed-to-parse-foreign-key-s-columns-w","errorCode":null,"errorMessage":"failed to parse foreign key %s columns: %w","messagePattern":"failed to parse foreign key (.+?) columns: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"agents/drivers/kingbase-go/kingbase_metadata.go","lineNumber":1739,"sourceCode":"\trows, err := s.metadataQuery(query)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\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{}","sourceCodeStart":1721,"sourceCodeEnd":1757,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/kingbase-go/kingbase_metadata.go#L1721-L1757","documentation":"Returned by listForeignKeysFromCatalog when parseCatalogAttributeNumbers fails to decode c.conkey (the foreign key's local column numbers) for a constraint while enumerating foreign keys from the system catalog (legacy V7 mode). The message names the constraint and wraps the \"invalid attribute number\" cause. The listing aborts rather than returning a foreign key with unknown columns.","triggerScenarios":"Calling the foreign-key listing API on a legacy Kingbase V7 server (s.mode.legacyV7 && !mysqlCompat) where the scanned c.conkey value is not a parseable integer list — corrupted or non-standard catalog formatting, or driver returning conkey in a type whose string form is not \"1 2 3\"-style.","commonSituations":"Legacy V7 Kingbase deployments where conkey serialization differs from modern versions; constraints created by migration tools that left unusual catalog values; driver upgrades changing the Go type conkey is scanned into.","solutions":["Check the wrapped \"invalid attribute number\" message for the exact offending token","Inspect the constraint's conkey in the catalog (SELECT conkey FROM sys_catalog.sys_constraint WHERE conname = ...) to confirm the raw format","Drop and re-add the offending foreign key constraint to regenerate conkey","Update the driver/compat-mode handling so conkey is scanned and parsed as the format this server emits"],"exampleFix":"// before: listing fails on one bad constraint\nfks, err := srv.ListForeignKeys(schema, table) // failed to parse foreign key fk_a columns: invalid attribute number \"~\"\n// after: recreate 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 that legacy catalog foreign keys have parseable conkey\nrows, _ := db.Query(`SELECT c.conname, c.conkey::text FROM sys_catalog.sys_constraint c WHERE c.contype = 'f'`)\nfor rows.Next() {\n    var name, conkey string\n    rows.Scan(&name, &conkey)\n    for _, tok := range strings.Fields(strings.Trim(conkey, \"{}\")) {\n        if _, err := strconv.Atoi(tok); err != nil {\n            log.Printf(\"FK %s has malformed conkey 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(), \"failed to parse foreign key\") && strings.Contains(err.Error(), \"columns:\") && !strings.Contains(err.Error(), \"referenced columns\") {\n        // local-column parse failure: identify and recreate the named constraint\n    }\n    return fmt.Errorf(\"list foreign keys: %w\", err)\n}","preventionTips":["On legacy V7 servers, validate FK catalog entries after migrations that create constraints programmatically","Avoid hand-editing sys_catalog rows; always recreate constraints via DDL","Pin and test the driver version against your oldest supported Kingbase release","Consider running FK listings in non-legacy (information_schema) mode when the server allows it"],"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-14T00:17:10.932Z"}