gastownhall/beads · error

db: RawSQL Query: %w

Error message

db: RawSQL Query: %w

What it means

RawSQLRepository.Query failed to execute the caller-supplied SQL query. The repository wraps the raw database/sql error, so the wrapped cause is whatever the driver returned for the given query string and args.

Source

Thrown at internal/storage/domain/db/raw_sql.go:23

	"fmt"

	"github.com/steveyegge/beads/internal/storage/domain"
)

func NewRawSQLRepository(runner Runner) domain.RawSQLRepository {
	return &rawSQLRepositoryImpl{runner: runner}
}

type rawSQLRepositoryImpl struct {
	runner Runner
}

var _ domain.RawSQLRepository = (*rawSQLRepositoryImpl)(nil)

func (r *rawSQLRepositoryImpl) Query(ctx context.Context, query string, args ...any) (*domain.RawSQLResult, error) {
	rows, err := r.runner.QueryContext(ctx, query, args...)
	if err != nil {
		return nil, fmt.Errorf("db: RawSQL Query: %w", err)
	}
	defer rows.Close()

	columns, err := rows.Columns()
	if err != nil {
		return nil, fmt.Errorf("db: RawSQL Query: columns: %w", err)
	}

	result := &domain.RawSQLResult{Columns: columns}
	for rows.Next() {
		values := make([]any, len(columns))
		ptrs := make([]any, len(columns))
		for i := range values {
			ptrs[i] = &values[i]
		}
		if err := rows.Scan(ptrs...); err != nil {
			return nil, fmt.Errorf("db: RawSQL Query: scan: %w", err)
		}

View on GitHub (pinned to 71377f2769)

Solutions

  1. Unwrap the error and read the driver message — it names the exact SQL problem (syntax, no such table, bind count)
  2. Validate the SQL against the actual schema and dialect of the configured database
  3. Match the number of ? placeholders to the number of args, with correct types
  4. Test the same query directly against the database CLI to isolate app vs data issues

Example fix

// before
rows, err := rawRepo.Query(ctx, "SELECT * FROM isues WHERE id = ?", id)
// after
rows, err := rawRepo.Query(ctx, "SELECT * FROM issues WHERE id = ?", id)
Defensive patterns

Strategy: validation

Validate before calling

// validate SQL and table names before executing
func validateSQL(q string) error {
    if strings.Contains(q, ";") { return errors.New("multiple statements not allowed") }
    if !allowedTablesRe.MatchString(q) { return errors.New("unknown table reference") }
    return nil
}
// check placeholder/arg count: strings.Count(q, "?") == len(args)

Try / catch

res, err := rawRepo.Query(ctx, q, args...)
if err != nil {
    log.Printf("raw query failed: %v (sql=%q)", errors.Unwrap(err), q)
    return err
}

Prevention

When it happens

Trigger: Calling Query with invalid SQL syntax, a non-existent table/column, wrong number or type of args for placeholders, or when the database is unreachable/locked.

Common situations: Hand-written SQL with typos; queries written for another dialect (MySQL vs Dolt/SQLite); positional placeholder count mismatched with args; read-only connections executing writes; missing migrations.

Related errors


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