gastownhall/beads · error

check %s.%s: %w

Error message

check %s.%s: %w

What it means

While scanning for severed clone-local foreign keys, scanSeveredCloneLocalFKs queries information_schema.TABLE_CONSTRAINTS to see if a constraint already exists. A missing constraint is normal (that's what the repair fixes), but a query failure — wrapped here as `check %s.%s` — aborts the whole scan.

Source

Thrown at cmd/bd/doctor/fix/clone_local_fks.go:89

	for _, fk := range CloneLocalFKs {
		var tables int
		if err := db.QueryRow(
			`SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?`,
			fk.Table,
		).Scan(&tables); err != nil {
			return nil, fmt.Errorf("check %s exists: %w", fk.Table, err)
		}
		if tables == 0 {
			continue
		}

		var constraints int
		if err := db.QueryRow(
			`SELECT COUNT(*) FROM information_schema.TABLE_CONSTRAINTS
			 WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND CONSTRAINT_NAME = ? AND CONSTRAINT_TYPE = 'FOREIGN KEY'`,
			fk.Table, fk.Constraint,
		).Scan(&constraints); err != nil {
			return nil, fmt.Errorf("check %s.%s: %w", fk.Table, fk.Constraint, err)
		}
		if constraints > 0 {
			continue
		}

		var orphans int
		//nolint:gosec // G201: identifiers come from the fixed CloneLocalFKs spec above, not user input.
		orphanCount := fmt.Sprintf(
			`SELECT COUNT(*) FROM %s t WHERE t.%s IS NOT NULL AND NOT EXISTS (SELECT 1 FROM %s r WHERE r.%s = t.%s)`,
			fk.Table, fk.Column, fk.RefTable, fk.RefColumn, fk.Column,
		)
		if err := db.QueryRow(orphanCount).Scan(&orphans); err != nil {
			return nil, fmt.Errorf("count %s orphans: %w", fk.Table, err)
		}

		severed = append(severed, SeveredCloneLocalFK{CloneLocalFK: fk, Orphans: orphans})
	}
	return severed, nil

View on GitHub (pinned to 71377f2769)

Solutions

  1. Read the wrapped driver error and fix the underlying DB connectivity/permission issue, then re-run `bd doctor`.
  2. Restart the Dolt-backed storage and confirm `bd doctor` can reach it.
  3. Grant the DB user read access to information_schema.TABLE_CONSTRAINTS.
  4. If the schema is corrupt, restore from git-exported .beads/issues.jsonl and re-import.
Defensive patterns

Strategy: retry

Validate before calling

var n int
err := db.QueryRow(`SELECT COUNT(*) FROM information_schema.TABLE_CONSTRAINTS`).Scan(&n)
if err != nil {
	// cannot read TABLE_CONSTRAINTS; fix privileges/connectivity first
	return err
}

Try / catch

if err := ScanSeveredCloneLocalFKs(ctx, db); err != nil && strings.Contains(err.Error(), "check") {
	// wrapped information_schema failure: back off and retry after fixing DB access
	return err
}

Prevention

When it happens

Trigger: The information_schema.TABLE_CONSTRAINTS COUNT(*) query in scanSeveredCloneLocalFKs (cmd/bd/doctor/fix/clone_local_fks.go:89) returns a driver error, e.g. during ScanSeveredCloneLocalFKs or the relink path in CloneLocalFKEnforcement.

Common situations: Dolt storage errors, interrupted database session, insufficient privileges to read TABLE_CONSTRAINTS, or schema corruption in a cloned workspace.

Understand the failure class

Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.

Related errors


AI-assisted analysis of gastownhall/beads@71377f2769 (2026-08-30). Data as JSON: /api/errors/27d0ef8d117a4aaf. Report an issue: GitHub.