t8y2/dbx · error

failed to parse foreign key %s columns: %w

Error message

failed to parse foreign key %s columns: %w

What it means

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.

Source

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

	rows, err := s.metadataQuery(query)
	if err != nil {
		return nil, err
	}
	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{}

View on GitHub (pinned to c0390bff16)

Solutions

  1. Check the wrapped "invalid attribute number" message for the exact offending token
  2. Inspect the constraint's conkey in the catalog (SELECT conkey FROM sys_catalog.sys_constraint WHERE conname = ...) to confirm the raw format
  3. Drop and re-add the offending foreign key constraint to regenerate conkey
  4. Update the driver/compat-mode handling so conkey is scanned and parsed as the format this server emits

Example fix

// before: listing fails on one bad constraint
fks, err := srv.ListForeignKeys(schema, table) // failed to parse foreign key fk_a columns: invalid attribute number "~"
// after: recreate 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 that legacy catalog foreign keys have parseable conkey
rows, _ := db.Query(`SELECT c.conname, c.conkey::text FROM sys_catalog.sys_constraint c WHERE c.contype = 'f'`)
for rows.Next() {
    var name, conkey string
    rows.Scan(&name, &conkey)
    for _, tok := range strings.Fields(strings.Trim(conkey, "{}")) {
        if _, err := strconv.Atoi(tok); err != nil {
            log.Printf("FK %s has malformed conkey token %q - recreate constraint", name, tok)
        }
    }
}

Try / catch

fks, err := srv.ListForeignKeys(schema, table)
if err != nil {
    if strings.Contains(err.Error(), "failed to parse foreign key") && strings.Contains(err.Error(), "columns:") && !strings.Contains(err.Error(), "referenced columns") {
        // local-column parse failure: identify and recreate the named constraint
    }
    return fmt.Errorf("list foreign keys: %w", err)
}

Prevention

When it happens

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

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

Understand the failure class

Related errors


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