ory/kratos · error

querying

Error message

querying %s

What it means

normalizeTable pages through rows (id, value) of the target table with RawQuery starting after lastID in batches. If the batch SELECT fails, the error is wrapped with "querying <table>". It marks the read phase of phone normalization as failed; the wrapped cause names the SQL/connection problem.

Solutions

  1. Read the wrapped cause for the exact SQL/driver error.
  2. Verify the DB user has SELECT permission on the target table.
  3. Confirm you are connected to the correct database/schema where the table exists.
  4. Re-run with a smaller --batch-size if timeouts occur on large tables; resume via --start-after.

Example fix

// before: wrong database
export DSN=postgres://user:pass@localhost:5432/otherdb?sslmode=disable
kratos migrate normalize-phone
// after
export DSN=postgres://user:pass@localhost:5432/kratos?sslmode=disable
kratos migrate normalize-phone
Defensive patterns

Strategy: retry

Validate before calling

// pre-flight: verify table access
_, err := conn.ExecContext(ctx, "SELECT 1 FROM identity_credentials LIMIT 1")
if err != nil { log.Fatalf("cannot read target table: %v", err) }

Type guard

null

Try / catch

if err := normalizePhoneNumbers(cmd, args); err != nil {
    if strings.Contains(err.Error(), "querying ") {
        // transient DB failure: back off and resume
        time.Sleep(10 * time.Second)
        return normalizePhoneNumbers(cmd, args)
    }
    return err
}

Prevention

When it happens

Trigger: Running phone normalization when the batched SELECT (table.selectQuery with lastID and batchSize parameters) fails — connection loss, SQL syntax/dialect mismatch, missing table, or permission denied on select.

Common situations: Running the migration against a database user lacking SELECT grants, pointing at a database where the table doesn't exist (wrong schema/database), or transient network drops during long batch runs.

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 ory/kratos@b86338da04 (2026-09-07). Data as JSON: /api/errors/dc025db4b8caca07. Report an issue: GitHub.

Appendix: source

Thrown at cmd/migrate/normalize_phone_handler.go:173

		total.skipped += s.skipped
		total.errors += s.errors
	}
	_, _ = fmt.Fprintf(tw, "TOTAL\t%d\t%d\t%d\t%d\n", total.scanned, total.updated, total.skipped, total.errors)
	_ = tw.Flush()
}

func normalizeTable(conn *pop.Connection, batchSize int, batchDelay time.Duration, startAfter uuid.UUID, dryRun bool, cmd *cobra.Command, table tableConfig) (normalizeStats, error) {
	var stats normalizeStats
	lastID := startAfter

	for {
		var rows []struct {
			ID    uuid.UUID `db:"id"`
			Value string    `db:"value"`
		}

		if err := conn.RawQuery(table.selectQuery, lastID, batchSize).All(&rows); err != nil {
			return stats, errors.Wrapf(err, "querying %s", table.name)
		}

		if len(rows) == 0 {
			break
		}

		for _, row := range rows {
			lastID = row.ID
			stats.scanned++

			// The SQL query pre-filters with LIKE '+%'. This Go-side regex additionally
			// rejects non-phone identifiers (e.g. OIDC subjects) that happen to start
			// with '+'. We cannot use SQL regex because SQLite does not support it.
			if !phonePattern.MatchString(row.Value) {
				stats.skipped++
				continue
			}

View on GitHub (pinned to b86338da04)