gastownhall/beads · error

failed to query cross-table duplicates: %w

Error message

failed to query cross-table duplicates: %w

What it means

CrossTableDuplicates removes stale issues-table rows whose IDs also exist in the wisps table. This error wraps a failure of the initial SELECT (`SELECT id FROM issues WHERE id IN (SELECT id FROM wisps)`) before any iteration begins — the query itself failed to execute, so the fix aborts.

Source

Thrown at cmd/bd/doctor/fix/validation.go:242

	if err != nil {
		return err
	}

	db, cfg, err := openDoltDB(beadsDir)
	if err != nil {
		fmt.Printf("  Cross-table duplicates fix skipped (%v)\n", err)
		return nil
	}
	defer db.Close()

	if skip, err := guardFixTarget("Cross-table duplicates fix", db, beadsDir, cfg); skip {
		return err
	}

	// Find IDs present in both tables — the wisp copy is canonical.
	rows, err := db.Query(`SELECT id FROM issues WHERE id IN (SELECT id FROM wisps)`)
	if err != nil {
		return fmt.Errorf("failed to query cross-table duplicates: %w", err)
	}
	var dupIDs []string
	for rows.Next() {
		var id string
		if err := rows.Scan(&id); err == nil {
			dupIDs = append(dupIDs, id)
		}
	}
	if err := rows.Err(); err != nil {
		_ = rows.Close()
		return fmt.Errorf("row iteration error: %w", err)
	}
	_ = rows.Close()

	if len(dupIDs) == 0 {
		fmt.Println("  No cross-table duplicates to fix")
		return nil
	}

View on GitHub (pinned to 71377f2769)

Solutions

  1. Run the schema migration / `bd doctor` schema fix so the wisps table exists before deduplication
  2. Confirm the Dolt server is up: `bd doctor` or `dolt sql -q 'SELECT 1'` against the configured database
  3. Check the config in .beads points at the intended database (hostname, port, database name)
  4. Grant the connecting user SELECT on both issues and wisps if permissions are the cause

Example fix

// before: query fails when wisps table is absent
rows, err := db.Query(`SELECT id FROM issues WHERE id IN (SELECT id FROM wisps)`)
// after: verify schema first
var hasWisps int
_ = db.QueryRow(`SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'wisps'`).Scan(&hasWisps)
if hasWisps == 0 {
    return nil // or run migration first
}
rows, err := db.Query(`SELECT id FROM issues WHERE id IN (SELECT id FROM wisps)`)
Defensive patterns

Strategy: validation

Validate before calling

// Verify both tables exist before running the dedup fix
q := `SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name IN ('issues','wisps')`
var n int
if err := db.QueryRow(q).Scan(&n); err != nil || n < 2 {
    return fmt.Errorf("schema not ready: need issues and wisps tables (got %d)", n)
}

Try / catch

rows, err := db.Query(`SELECT id FROM issues WHERE id IN (SELECT id FROM wisps)`)
if err != nil {
    if mysqlErr, ok := err.(*mysql.MySQLError); ok && mysqlErr.Number == 1146 {
        return nil // wisps table absent: nothing to dedupe
    }
    return fmt.Errorf("failed to query cross-table duplicates: %w", err)
}

Prevention

When it happens

Trigger: db.Query fails because a referenced table is missing (wisps table doesn't exist in an older database), the connection to the Dolt server is down, or the SQL is rejected server-side (permissions, corrupt metadata).

Common situations: Running the fix against a repo created before the wisps table was introduced (schema migration not applied); Dolt server not running or wrong database selected; user lacks SELECT on wisps.

Understand the failure class

Background: "query failed", "%w: SQL error" — wrapped database query errors in Go libraries explained — this error's family across 3 libraries.

Related errors


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