clockworklabs/SpacetimeDB · error

Caller is not authorized to run SQL DML statements

Error message

Caller {} is not authorized to run SQL DML statements

What it means

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.

Solutions

  1. Authenticate as the database owner (or an identity that was granted write access) and retry the statement
  2. 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)
  3. For programmatic access, mint a token for an authorized identity and attach it to the SQL request

Example fix

# before: anonymous or unauthorized caller
spacetime sql my_db "insert into t values (1)"
# error: Caller 0x...c7f0 is not authorized to run SQL DML statements

# after: authenticate as an identity with write access
spacetime login
spacetime sql my_db "insert into t values (1)"
Defensive patterns

Strategy: validation

Validate before calling

// Before submitting SQL, ensure the caller can write if the statement mutates
fn require_write_access_for(stmt: &Statement, auth: &AuthCtx) -> Result<(), anyhow::Error> {
    if matches!(stmt, Statement::DML(_)) && !auth.has_write_access() {
        Err(anyhow::anyhow!(
            "caller {} cannot run DML; authenticate or request write permission",
            auth.caller()
        ))
    } else {
        Ok(())
    }
}

Try / catch

match sql_result {
    Err(e) if e.to_string().contains("not authorized to run SQL DML") => {
        // Re-authenticate as the owner, or have the owner grant write access, then retry
    }
    other => other,
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of clockworklabs/SpacetimeDB@eddf9f5014 (2026-09-20). Data as JSON: /api/errors/3b073915e82f7672. Report an issue: GitHub.

Appendix: source

Thrown at crates/core/src/sql/execute.rs:147

                Ok(plan)
            })?;

            // Update transaction metrics
            tx.metrics.merge(metrics);

            Ok((
                SqlResult {
                    tx_offset,
                    rows,
                    metrics: tx.metrics,
                },
                trapped,
            ))
        }
        Statement::DML(stmt) => {
            // An extra layer of auth is required for DML
            if !auth.has_write_access() {
                return Err(anyhow!("Caller {} is not authorized to run SQL DML statements", auth.caller()).into());
            }
            if stmt.table_id() == spacetimedb_datastore::system_tables::ST_ENV_ID {
                return Err(anyhow!("Database environment variables can only be changed by publishing").into());
            }

            // Evaluate the mutation
            let (mut tx, _) = db.with_auto_rollback(tx, |tx| execute_dml_stmt(&auth, stmt, tx, &mut metrics))?;

            // Update transaction metrics
            tx.metrics.merge(metrics);

            // Update views
            let (result, _num_views_evaluated, trapped) = match instance {
                Some(instance) => ModuleHost::call_views_with_tx(tx, instance, auth.caller()),
                None => (ViewCallResult::default(tx), 0, false),
            };

            // Rollback transaction and report metrics if view execution failed

View on GitHub (pinned to eddf9f5014)