t8y2/dbx · error

failed to parse foreign key %s referenced columns: %w

Error message

failed to parse foreign key %s referenced columns: %w

What it means

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.

Source

Thrown at agents/drivers/kingbase-go/kingbase_metadata.go:1743

	defer rows.Close()
	type rawForeignKey struct {
		name, refSchema, refTable string
		columns, refColumns       []int
	}
	rawKeys := []rawForeignKey{}
	for rows.Next() {
		var item rawForeignKey
		var columnsRaw, refColumnsRaw any
		if err := rows.Scan(&item.name, &columnsRaw, &refColumnsRaw, &item.refSchema, &item.refTable); err != nil {
			return nil, err
		}
		item.columns, err = parseCatalogAttributeNumbers(columnsRaw)
		if err != nil {
			return nil, fmt.Errorf("failed to parse foreign key %s columns: %w", item.name, err)
		}
		item.refColumns, err = parseCatalogAttributeNumbers(refColumnsRaw)
		if err != nil {
			return nil, fmt.Errorf("failed to parse foreign key %s referenced columns: %w", item.name, err)
		}
		rawKeys = append(rawKeys, item)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	if len(rawKeys) == 0 {
		return []foreignKeyInfo{}, nil
	}
	localAttributes, err := s.relationAttributesByNumber(catalog, prefix, schema, table)
	if err != nil {
		return nil, err
	}
	refAttributes := map[string]map[int]string{}
	result := []foreignKeyInfo{}
	for _, raw := range rawKeys {
		key := raw.refSchema + "\x00" + raw.refTable
		attributes := refAttributes[key]

View on GitHub (pinned to c0390bff16)

Solutions

  1. Read the wrapped "invalid attribute number" token to identify the unparseable content
  2. Verify confkey directly: SELECT confkey FROM sys_catalog.sys_constraint WHERE conname = '<constraint from message>'
  3. Recreate the foreign key constraint so the catalog regenerates a clean numeric confkey
  4. Upgrade/adjust the driver or compat mode so confkey is delivered in the expected string form

Example fix

// before: referenced-column parse failure aborts listing
fks, err := srv.ListForeignKeys(schema, table) // failed to parse foreign key fk_a referenced columns: invalid attribute number "n"
// after: rebuild the constraint
// ALTER TABLE t DROP CONSTRAINT fk_a; ALTER TABLE t ADD CONSTRAINT fk_a FOREIGN KEY (col) REFERENCES p(id);
fks, err := srv.ListForeignKeys(schema, table)
Defensive patterns

Strategy: try-catch

Validate before calling

// Go: pre-check confkey parseability on legacy servers
rows, _ := db.Query(`SELECT c.conname, c.confkey::text FROM sys_catalog.sys_constraint c WHERE c.contype = 'f'`)
for rows.Next() {
    var name, confkey string
    rows.Scan(&name, &confkey)
    for _, tok := range strings.Fields(strings.Trim(confkey, "{}")) {
        if _, err := strconv.Atoi(tok); err != nil {
            log.Printf("FK %s has malformed confkey token %q - recreate constraint", name, tok)
        }
    }
}

Try / catch

fks, err := srv.ListForeignKeys(schema, table)
if err != nil {
    if strings.Contains(err.Error(), "referenced columns") {
        // confkey parse failure: the referenced-side column list is malformed;
        // drop/re-add the constraint named in the message
    }
    return fmt.Errorf("list foreign keys: %w", err)
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Related errors


AI-assisted analysis of t8y2/dbx@c0390bff16 (2026-09-05). Data as JSON: /api/errors/26eb4b12fbadeaa0. Report an issue: GitHub.