{"record":{"id":"520f3b901a1e82e6","repo":"tursodatabase/turso","slug":"sql-execution-failed-520f3b","errorCode":null,"errorMessage":"SQL execution failed","messagePattern":"SQL execution failed","errorType":"exception","errorClass":"TursoServerlessException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Serverless.Client/TursoSession.cs","lineNumber":127,"sourceCode":"                        rows.Add(new TursoRow(values, rowColumns ?? []));\n                    }\n\n                    break;\n                case \"step_end\":\n                    if (entry.AffectedRowCount is { } affected)\n                    {\n                        rowsAffected = affected;\n                    }\n\n                    if (entry.LastInsertRowid is { } rowid)\n                    {\n                        lastInsertRowid = rowid;\n                    }\n\n                    break;\n                case \"step_error\":\n                case \"error\":\n                    throw new TursoServerlessException(entry.Error?.Message ?? \"SQL execution failed\", entry.Error?.Code);\n            }\n        }\n\n        UpdateAutocommitForTransactionControlStatement(sql);\n        return new TursoResultSet(columns, columnTypes, rows, rowsAffected, lastInsertRowid);\n    }\n\n    internal async Task<TursoBatchResult> BatchAsync(IReadOnlyList<HranaStatement> statements, TursoBatchMode? mode, TimeSpan? queryTimeout, CancellationToken cancellationToken)\n    {\n        var userSteps = statements.Select(static s => new HranaBatchStep { Statement = s }).ToList();\n\n        List<HranaBatchStep> steps;\n        var firstUserStepIdx = 0;\n        var lastUserStepIdx = userSteps.Count - 1;\n        var beginIdx = -1;\n        var commitIdx = -1;\n        var rollbackIdx = -1;\n","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/tursodatabase/turso/blob/244cde92a7df7f9b8b8b7a4075c35a12977e303e/bindings/dotnet/src/Turso.Serverless.Client/TursoSession.cs#L109-L145","documentation":"ExecuteAsync streams a cursor batch and inspects each result entry. An entry of type step_error or error throws TursoServerlessException with the server's message, falling back to 'SQL execution failed' only when the error entry carried no message. It marks a statement that failed on the server at execute time.","triggerScenarios":"Runtime SQL failures: UNIQUE/FOREIGN KEY/CHECK constraint violations, NOT NULL on insert, type affinity errors, or accessing a locked table — e.g. INSERT duplicating a primary key that validation did not catch.","commonSituations":"Concurrent writers violating constraints, payload regressions feeding bad data, statements valid against one schema but failing against another, transaction conflicts on busy databases.","solutions":["Inspect the exception Message and Code (e.g. SQLITE_CONSTRAINT_UNIQUE) — the server message is present in the common case","Fix the data or statement per the constraint named by the code; add pre-validation for the specific constraint","For lock/busy codes, retry with backoff or shorten the transaction","Reproduce with turso db shell against the same database and row"],"exampleFix":"// before\nawait conn.ExecuteAsync(\"INSERT INTO users(id) VALUES (@id)\", cmd); // duplicate id\n\n// after\nvar exists = await conn.ExecuteAsync(\"SELECT 1 FROM users WHERE id=@id\", cmd);\nif (exists.Rows.Count == 0)\n    await conn.ExecuteAsync(\"INSERT INTO users(id) VALUES (@id)\", cmd);","handlingStrategy":"try-catch","validationCode":"if (await ExistsAsync(conn, \"users\", row.Id)) return; // pre-check for the common UNIQUE case","typeGuard":null,"tryCatchPattern":"try { await conn.ExecuteAsync(sql, cmd, timeout, ct); } catch (TursoServerlessException ex) when (ex.Code?.StartsWith(\"SQLITE_CONSTRAINT\") == true) { log.Warn($\"constraint {ex.Code}: {ex.Message}\"); throw; } catch (TursoServerlessException ex) when (ex.Code is \"SQLITE_BUSY\" or \"SQLITE_LOCKED\") { await Task.Delay(backoff); await conn.ExecuteAsync(sql, cmd, timeout, ct); }","preventionTips":["Pre-validate uniqueness/NOT NULL constraints for user-supplied data","Keep transactions short to limit lock contention","Always log ex.Code — it disambiguates constraint vs lock vs syntax"],"tags":["csharp","dotnet","sql","constraint-violation","hrana-protocol"],"backgroundTag":"sql-execution-error","analyzedSha":"244cde92a7df7f9b8b8b7a4075c35a12977e303e","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}