clockworklabs/SpacetimeDB · error · anyhow::Error

Multiple databases found in config: {}. Please specify which

Error message

Multiple databases found in config: {}. Please specify which database to query:
  spacetime sql <database> "{}"

What it means

When `spacetime sql` gets exactly one positional that contains a space, it assumes the argument is a SQL query rather than a database name. It then looks at the project config database targets: if more than one database is configured and no explicit database was given, there is no way to pick a target, so it errors listing the configured database names and echoing the query back in a corrected command line.

Source

Thrown at crates/cli/src/subcommands/sql.rs:265

            "query",
            "spacetime sql [database] <query> [--no-config]",
        )
        .or_else(|e| {
            // `sql` expects exactly 1 query arg, so if we have 2+ positional args the first
            // must be a database name. If it didn't match any config target, treat it as an
            // ad-hoc database outside the project (auto-fallthrough).
            if raw_parts.len() >= 2 {
                Ok(ResolvedDbArgs {
                    database: raw_parts[0].clone(),
                    server: None,
                    remaining_args: raw_parts[1..].to_vec(),
                })
            } else if raw_parts.len() == 1 && raw_parts[0].contains(' ') {
                // The single arg contains spaces, so it's almost certainly a SQL query,
                // not a database name. Give a clearer error than "missing <query>".
                let targets = config_targets.as_deref().unwrap_or_default();
                let known: Vec<&str> = targets.iter().map(|t| t.database.as_str()).collect();
                Err(anyhow::anyhow!(
                    "Multiple databases found in config: {}. Please specify which database to query:\n  \
                     spacetime sql <database> \"{}\"",
                    known.join(", "),
                    raw_parts[0]
                ))
            } else {
                Err(e)
            }
        })?;
        let query = resolved.remaining_args.join(" ");
        let confirmed = args.get_one::<bool>("confirmed").copied();

        let con = parse_req(config, args, &resolved.database, resolved.server.as_deref()).await?;
        let mut api = ClientApi::new(con).sql();
        if let Some(confirmed) = confirmed {
            api = api.query(&[("confirmed", if confirmed { "true" } else { "false" })]);
        }

View on GitHub (pinned to 524b4487d9)

Solutions

  1. Re-run with the database name first, exactly as the message shows: `spacetime sql <database> "<query>"`
  2. Set/keep a single default database in the project config so the disambiguation disappears
  3. Use `--no-config` if you intended an ad-hoc server database rather than the project's configured targets
  4. Tab-complete the database name to avoid typos against the names printed in the error

Example fix

# before
spacetime sql "SELECT * FROM player"        # config has db1, db2
# after
spacetime sql db1 "SELECT * FROM player"
Defensive patterns

Strategy: validation

Validate before calling

# Detect multi-database configs before running ad-hoc queries
n=$(toml-get project.databases 2>/dev/null | wc -l)   # or count entries however your config is shaped
[ "$n" -le 1 ] || echo 'multiple db targets: always pass <database> explicitly'

Try / catch

// Wrapper: on the ambiguity error, surface a database picker instead of failing
let out = run_cli("spacetime sql \"SELECT 1\"");
if out.contains("Multiple databases found in config") { pick_db_and_retry(); }

Prevention

When it happens

Trigger: Running `spacetime sql "SELECT * FROM my_table"` from a project directory whose config declares multiple database targets, without naming which database to run against.

Common situations: Monorepo or workspace config with several databases (dev/test/prod); newly added second database in a project template; assuming the CLI remembers a 'current' database from a previous invocation.

Related errors


AI-assisted analysis of clockworklabs/SpacetimeDB@524b4487d9 (2026-08-16). Data as JSON: /api/errors/edaf1f23b82d4f36. Report an issue: GitHub.