nautechsystems/nautilus_trader · error

Failed to check if order initialized exists: {e}

Error message

Failed to check if order initialized exists: {e}

What it means

Wraps sqlx failures from the `SELECT EXISTS(...)` query that checks whether an `OrderInitialized` event row exists for a `client_order_id` in the `order_event` table. The library converts the driver error into anyhow context so callers can distinguish a query failure from a simple `false` answer. Note the boolean result is always `Ok` when the query succeeds; this error only fires when the query itself cannot be executed.

Source

Thrown at crates/infrastructure/src/sql/queries.rs:587

    }

    /// Checks if an `OrderInitialized` event exists for the given `client_order_id` via the provided `pool`.
    ///
    /// # Errors
    ///
    /// Returns an error if the SQL SELECT operation fails.
    pub async fn check_if_order_initialized_exists(
        pool: &PgPool,
        client_order_id: ClientOrderId,
    ) -> anyhow::Result<bool> {
        sqlx::query(r#"
            SELECT EXISTS(SELECT 1 FROM "order_event" WHERE client_order_id = $1 AND kind = 'OrderInitialized')
        "#)
            .bind(client_order_id.to_string())
            .fetch_one(pool)
            .await
            .map(|row| row.get(0))
            .map_err(|e| anyhow::anyhow!("Failed to check if order initialized exists: {e}"))
    }

    /// Checks if any account event exists for the given `account_id` via the provided `pool`.
    ///
    /// # Errors
    ///
    /// Returns an error if the SQL SELECT operation fails.
    pub async fn check_if_account_event_exists(
        pool: &PgPool,
        account_id: AccountId,
    ) -> anyhow::Result<bool> {
        sqlx::query(
            r#"
            SELECT EXISTS(SELECT 1 FROM "account_event" WHERE account_id = $1)
        "#,
        )
        .bind(account_id.to_string())
        .fetch_one(pool)

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Check DB connectivity with a trivial query on the same pool.
  2. Confirm the `order_event` table exists and has `client_order_id` and `kind` columns (run migrations).
  3. Verify the `client_order_id.to_string()` matches the stored id format.
  4. Read the inner sqlx error in `{e}` for the exact driver cause and retry if transient.

Example fix

// before
let exists = check_if_order_initialized_exists(&pool, client_order_id).await?;
// after
let exists = check_if_order_initialized_exists(&pool, client_order_id)
    .await
    .with_context(|| format!("checking init for {client_order_id}"))?;
Defensive patterns

Strategy: try-catch

Validate before calling

// Ensure table exists before checking
sqlx::query(r#"SELECT EXISTS(SELECT 1 FROM "order_event" LIMIT 1)"#)
    .fetch_one(pool).await
    .map_err(|e| anyhow::anyhow!("order_event table unavailable: {e}"))?;

Type guard

fn is_schema_error(e: &anyhow::Error) -> bool {
    let s = e.to_string();
    s.contains("does not exist") || s.contains("relation") || s.contains("column")
}

Try / catch

match check_if_order_initialized_exists(&pool, client_order_id).await {
    Ok(exists) => { /* exists is a plain bool */ }
    Err(e) if is_schema_error(&e) => { /* run migrations, then retry */ }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Calling `check_if_order_initialized_exists(pool, client_order_id)` with an unreachable database, a missing/renamed `order_event` table or `kind` column, or a client_order_id whose string form is incompatible with the column type.

Common situations: Schema not migrated before first use; connecting to the wrong database or schema; connection pool exhausted after idle timeout; case-sensitivity issues with the quoted `"order_event"` table name.

Understand the failure class

Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/ad60c54ea2d9c3fd. Report an issue: GitHub.