{"record":{"id":"ee2367089bd4d23d","repo":"tursodatabase/turso","slug":"sql-execution-failed","errorCode":null,"errorMessage":"SQL execution failed","messagePattern":"SQL execution failed","errorType":"exception","errorClass":"DatabaseError","httpStatus":null,"severity":"error","filePath":"serverless/javascript/src/session.ts","lineNumber":385,"sourceCode":"          if (entry.row) {\n            const decodedRow = entry.row.map(value => decodeValue(value, safeIntegers));\n            const rowObject = this.createRowObject(decodedRow, columns);\n            rows.push(rowObject);\n          }\n          break;\n        case 'step_end':\n          if (entry.affected_row_count !== undefined) {\n            rowsAffected = entry.affected_row_count;\n          }\n          if (entry.last_insert_rowid !== undefined && entry.last_insert_rowid !== null) {\n            lastInsertRowid = typeof entry.last_insert_rowid === 'number'\n              ? entry.last_insert_rowid\n              : parseInt(entry.last_insert_rowid, 10);\n          }\n          break;\n        case 'step_error':\n        case 'error':\n          throw new DatabaseError(entry.error?.message || 'SQL execution failed', entry.error?.code);\n      }\n    }\n\n    return {\n      columns,\n      columnTypes,\n      rows,\n      rowsAffected,\n      lastInsertRowid\n    };\n  }\n\n  /**\n   * Create a row object with both array and named property access.\n   * \n   * @param values - Array of column values\n   * @param columns - Array of column names\n   * @returns Row object with dual access patterns","sourceCodeStart":367,"sourceCodeEnd":403,"githubUrl":"https://github.com/tursodatabase/turso/blob/bad083fafbefdeae9a42ec19bdaaad8918dcf411/serverless/javascript/src/session.ts#L367-L403","documentation":"DatabaseError thrown while consuming the cursor stream of Session.execute() — the path behind Connection/Transaction run/get/all and Statement execution — when the server sends a step_error or fatal error entry. The server's message is preferred; the literal 'SQL execution failed' is the fallback for entries that carry no error.message.","triggerScenarios":"Runtime SQL failures surfacing from db.run()/db.get()/db.all() or tx equivalents: UNIQUE/NOT NULL constraint violations, 'no such table/column', SQLITE_BUSY write contention; malformed message-less error entries from a buggy server triggering the fallback text.","commonSituations":"INSERTs violating constraints with unvalidated data; referencing tables that don't exist in the deployed database; concurrent writers hitting lock timeouts; server bugs emitting errors without messages.","solutions":["Read the server message and DatabaseError.code (e.g. SQLITE_CONSTRAINT_UNIQUE) — they identify the failing statement and reason","Fix the data or schema the message points to (ON CONFLICT clauses, migrations, validated input)","If only the fallback text appears, capture the raw response via a logging proxy and report the server issue"],"exampleFix":"// before\nawait db.run(\"INSERT INTO users(id) VALUES (?)\", 1); // duplicate id -> step_error\n\n// after\nawait db.run(\"INSERT INTO users(id) VALUES (?) ON CONFLICT(id) DO NOTHING\", 1);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"const isDatabaseError = (e: unknown): e is Error & { code?: string } =>\n  e instanceof Error && e.name === \"DatabaseError\";","tryCatchPattern":"try {\n  await db.run(sql, ...args);\n} catch (e) {\n  const code = (e as { code?: string })?.code;\n  if (code === \"SQLITE_CONSTRAINT_UNIQUE\") {\n    // duplicate key: upsert or skip\n  } else if (code === \"SQLITE_BUSY\") {\n    // write contention: back off and retry\n  } else throw e;\n}","preventionTips":["Branch on DatabaseError.code rather than parsing messages — codes are stable","Validate input data (uniqueness, NOT NULL, FK targets) before binding","Wrap groups of dependent writes in transactionAsync so a failure rolls the group back"],"tags":["sql","runtime-error","protocol","javascript"],"backgroundTag":"sql-execution-error","analyzedSha":"bad083fafbefdeae9a42ec19bdaaad8918dcf411","analyzedAt":"2026-08-16T23:12:11.798Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}