{"record":{"id":"b04abc13a394b54e","repo":"gastownhall/beads","slug":"failed-to-query-cross-table-duplicates-w","errorCode":null,"errorMessage":"failed to query cross-table duplicates: %w","messagePattern":"failed to query cross-table duplicates: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/bd/doctor/fix/validation.go","lineNumber":242,"sourceCode":"\tif err != nil {\n\t\treturn err\n\t}\n\n\tdb, cfg, err := openDoltDB(beadsDir)\n\tif err != nil {\n\t\tfmt.Printf(\"  Cross-table duplicates fix skipped (%v)\\n\", err)\n\t\treturn nil\n\t}\n\tdefer db.Close()\n\n\tif skip, err := guardFixTarget(\"Cross-table duplicates fix\", db, beadsDir, cfg); skip {\n\t\treturn err\n\t}\n\n\t// Find IDs present in both tables — the wisp copy is canonical.\n\trows, err := db.Query(`SELECT id FROM issues WHERE id IN (SELECT id FROM wisps)`)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to query cross-table duplicates: %w\", err)\n\t}\n\tvar dupIDs []string\n\tfor rows.Next() {\n\t\tvar id string\n\t\tif err := rows.Scan(&id); err == nil {\n\t\t\tdupIDs = append(dupIDs, id)\n\t\t}\n\t}\n\tif err := rows.Err(); err != nil {\n\t\t_ = rows.Close()\n\t\treturn fmt.Errorf(\"row iteration error: %w\", err)\n\t}\n\t_ = rows.Close()\n\n\tif len(dupIDs) == 0 {\n\t\tfmt.Println(\"  No cross-table duplicates to fix\")\n\t\treturn nil\n\t}","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/cmd/bd/doctor/fix/validation.go#L224-L260","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Run the schema migration / `bd doctor` schema fix so the wisps table exists before deduplication","Confirm the Dolt server is up: `bd doctor` or `dolt sql -q 'SELECT 1'` against the configured database","Check the config in .beads points at the intended database (hostname, port, database name)","Grant the connecting user SELECT on both issues and wisps if permissions are the cause"],"exampleFix":"// before: query fails when wisps table is absent\nrows, err := db.Query(`SELECT id FROM issues WHERE id IN (SELECT id FROM wisps)`)\n// after: verify schema first\nvar hasWisps int\n_ = db.QueryRow(`SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'wisps'`).Scan(&hasWisps)\nif hasWisps == 0 {\n    return nil // or run migration first\n}\nrows, err := db.Query(`SELECT id FROM issues WHERE id IN (SELECT id FROM wisps)`)","handlingStrategy":"validation","validationCode":"// Verify both tables exist before running the dedup fix\nq := `SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name IN ('issues','wisps')`\nvar n int\nif err := db.QueryRow(q).Scan(&n); err != nil || n < 2 {\n    return fmt.Errorf(\"schema not ready: need issues and wisps tables (got %d)\", n)\n}","typeGuard":null,"tryCatchPattern":"rows, err := db.Query(`SELECT id FROM issues WHERE id IN (SELECT id FROM wisps)`)\nif err != nil {\n    if mysqlErr, ok := err.(*mysql.MySQLError); ok && mysqlErr.Number == 1146 {\n        return nil // wisps table absent: nothing to dedupe\n    }\n    return fmt.Errorf(\"failed to query cross-table duplicates: %w\", err)\n}","preventionTips":["Run schema migrations before dedup/fix routines","Grant SELECT on both issues and wisps to the connecting DB user","Confirm the config points at the live, current database","Run a quick `SELECT 1` smoke test before expensive fix queries"],"tags":["database","sql-query","mysql","dolt"],"backgroundTag":"sql-query-failed","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}