drizzle-team/drizzle-orm · error · Error

${it.code}: ${it.message}

Error message

${it.code}: ${it.message}

What it means

Surfaced by the Cloudflare D1 HTTP driver in drizzle-kit. After POSTing a single SQL statement to the D1 REST endpoint, the SDK inspects `data.success`; when Cloudflare returns `success: false` it concatenates each `{code, message}` error pair into a single thrown Error. This is the remote API's error payload re-thrown locally, so the originating code is Cloudflare's (auth, SQL syntax, database state) rather than drizzle's.

Source

Thrown at drizzle-kit/src/cli/connections.ts:1054

			) => {
				const res = await fetch(
					`https://api.cloudflare.com/client/v4/accounts/${credentials.accountId}/d1/database/${credentials.databaseId}/${
						method === 'values' ? 'raw' : 'query'
					}`,
					{
						method: 'POST',
						body: JSON.stringify({ sql, params }),
						headers: {
							'Content-Type': 'application/json',
							Authorization: `Bearer ${credentials.token}`,
						},
					},
				);

				const data = (await res.json()) as D1Response;

				if (!data.success) {
					throw new Error(
						data.errors.map((it) => `${it.code}: ${it.message}`).join('\n'),
					);
				}

				const result = data.result[0].results;
				const rows = Array.isArray(result) ? result : result.rows;

				return {
					rows,
				};
			};

			const remoteBatchCallback = async (
				queries: {
					sql: string;
				}[],
			) => {
				const sql = queries.map((q) => q.sql).join('; ');

View on GitHub (pinned to b7862528fd)

Solutions

  1. Inspect the concatenated `<code>: <message>` text; Cloudflare error codes (e.g. 8000011 invalid token, 7000/7003 SQL errors) point at the root cause.
  2. Verify the token has the `D1 Edit` / `Account D1 write` permission and is not expired.
  3. Confirm `accountId` and `databaseId` in `drizzle.config.ts` match a database listed via `wrangler d1 list`.
  4. If it is a SQL/schema error, run `drizzle-kit migrate` again or fix the offending statement.

Example fix

// before
token: process.env.CF_API_TOKEN // wrong-scoped token
// after - regenerate token granting D1 Edit on the account
token: process.env.CF_D1_TOKEN
Defensive patterns

Strategy: try-catch

Validate before calling

// Validate D1 credentials before first query
function assertD1Creds(c: { accountId: string; databaseId: string; token: string }) {
  if (!c.accountId || !c.databaseId || !c.token) throw new Error('Incomplete D1 credentials');
}

Type guard

function isD1ErrorResponse(d: any): d is { success: false; errors: { code: number; message: string }[] } {
  return d && d.success === false && Array.isArray(d.errors);
}

Try / catch

try {
  const res = await studioDb.execute(sql`...`);
} catch (e) {
  const msg = (e as Error).message;
  // msg is `<code>: <message>` joined by newlines; parse the first code
  const code = parseInt(msg.split(':')[0]!, 10);
  if (code === 8000011) console.error('Token invalid/expired - regenerate it');
  throw e;
}

Prevention

When it happens

Trigger: Configuring `driver: 'd1-http'` with `accountId`, `databaseId`, and `token`, then executing any query through Studio or migrate whose SQL is rejected by D1 (syntax error, missing table, insufficient token scopes, wrong/invalid databaseId).

Common situations: Expired or wrong-scoped Cloudflare API token (code 10000/8000011), mistyped `databaseId`/`accountId`, schema drift (querying a table not yet migrated), or a SQL syntax error generated by a stale drizzle schema.

Related errors


AI-assisted analysis of drizzle-team/drizzle-orm@b7862528fd (2026-08-03). Data as JSON: /data/errors/dd99d6f3b010d83e.json. Report an issue: GitHub.