{"record":{"id":"3b073915e82f7672","repo":"clockworklabs/SpacetimeDB","slug":"caller-is-not-authorized-to-run-sql-dml-stateme","errorCode":null,"errorMessage":"Caller {} is not authorized to run SQL DML statements","messagePattern":"Caller (.+?) is not authorized to run SQL DML statements","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/core/src/sql/execute.rs","lineNumber":147,"sourceCode":"                Ok(plan)\n            })?;\n\n            // Update transaction metrics\n            tx.metrics.merge(metrics);\n\n            Ok((\n                SqlResult {\n                    tx_offset,\n                    rows,\n                    metrics: tx.metrics,\n                },\n                trapped,\n            ))\n        }\n        Statement::DML(stmt) => {\n            // An extra layer of auth is required for DML\n            if !auth.has_write_access() {\n                return Err(anyhow!(\"Caller {} is not authorized to run SQL DML statements\", auth.caller()).into());\n            }\n            if stmt.table_id() == spacetimedb_datastore::system_tables::ST_ENV_ID {\n                return Err(anyhow!(\"Database environment variables can only be changed by publishing\").into());\n            }\n\n            // Evaluate the mutation\n            let (mut tx, _) = db.with_auto_rollback(tx, |tx| execute_dml_stmt(&auth, stmt, tx, &mut metrics))?;\n\n            // Update transaction metrics\n            tx.metrics.merge(metrics);\n\n            // Update views\n            let (result, _num_views_evaluated, trapped) = match instance {\n                Some(instance) => ModuleHost::call_views_with_tx(tx, instance, auth.caller()),\n                None => (ViewCallResult::default(tx), 0, false),\n            };\n\n            // Rollback transaction and report metrics if view execution failed","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/eddf9f5014579a50d4b67630e28b6e15cad9c4af/crates/core/src/sql/execute.rs#L129-L165","documentation":"SQL execution requires an extra authorization layer for DML statements (INSERT/UPDATE/DELETE): auth.has_write_access() must be true for the AuthCtx caller, on top of any row/table-level policy. Anonymous callers or tokens without write permission can run SELECTs but are rejected on DML, with the caller identity included in the message.","triggerScenarios":"Running INSERT/UPDATE/DELETE via spacetime sql or the HTTP SQL endpoint while unauthenticated (anonymous identity), or with a token that was never granted write access to that database.","commonSituations":"A fresh spacetime login identity operating on a database owned by someone else; scripts that forgot to set credentials; tokens minted with read-only permissions.","solutions":["Authenticate as the database owner (or an identity that was granted write access) and retry the statement","Grant write access to the caller identity shown in the error via the permission system (spacetime permission grant, see --help for exact permission names in your version)","For programmatic access, mint a token for an authorized identity and attach it to the SQL request"],"exampleFix":"# before: anonymous or unauthorized caller\nspacetime sql my_db \"insert into t values (1)\"\n# error: Caller 0x...c7f0 is not authorized to run SQL DML statements\n\n# after: authenticate as an identity with write access\nspacetime login\nspacetime sql my_db \"insert into t values (1)\"","handlingStrategy":"validation","validationCode":"// Before submitting SQL, ensure the caller can write if the statement mutates\nfn require_write_access_for(stmt: &Statement, auth: &AuthCtx) -> Result<(), anyhow::Error> {\n    if matches!(stmt, Statement::DML(_)) && !auth.has_write_access() {\n        Err(anyhow::anyhow!(\n            \"caller {} cannot run DML; authenticate or request write permission\",\n            auth.caller()\n        ))\n    } else {\n        Ok(())\n    }\n}","typeGuard":null,"tryCatchPattern":"match sql_result {\n    Err(e) if e.to_string().contains(\"not authorized to run SQL DML\") => {\n        // Re-authenticate as the owner, or have the owner grant write access, then retry\n    }\n    other => other,\n}","preventionTips":["Check credentials are attached before running DML (spacetime whoami / token env)","Grant least-privilege permissions deliberately via the permission system","In scripts, fail fast on auth errors instead of retrying blindly"],"tags":["spacetimedb","sql","authorization","dml","permissions"],"backgroundTag":"sql-permission-denied","analyzedSha":"eddf9f5014579a50d4b67630e28b6e15cad9c4af","analyzedAt":"2026-09-20T12:15:59.611Z","contentChangedAt":"2026-09-20T12:15:59.611Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}