SeaQL/sea-orm · error

Disconnected

Error message

Disconnected

What it means

get_database_backend() reports which backend (Postgres, MySQL, Sqlite, ...) a connection uses. When the DatabaseConnection is in the Disconnected state (never connected, or the connection was closed/taken), there is no backend to report, so the library panics with "Disconnected".

Source

Thrown at sea-orm-sync/src/database/db_connection.rs:678

    ///
    /// # Panics
    ///
    /// Panics if [DatabaseConnection] is `Disconnected`.
    pub fn get_database_backend(&self) -> DbBackend {
        match &self.inner {
            #[cfg(feature = "sqlx-mysql")]
            DatabaseConnectionType::SqlxMySqlPoolConnection(_) => DbBackend::MySql,
            #[cfg(feature = "sqlx-postgres")]
            DatabaseConnectionType::SqlxPostgresPoolConnection(_) => DbBackend::Postgres,
            #[cfg(feature = "sqlx-sqlite")]
            DatabaseConnectionType::SqlxSqlitePoolConnection(_) => DbBackend::Sqlite,
            #[cfg(feature = "rusqlite")]
            DatabaseConnectionType::RusqliteSharedConnection(_) => DbBackend::Sqlite,
            #[cfg(feature = "mock")]
            DatabaseConnectionType::MockDatabaseConnection(conn) => conn.get_database_backend(),
            #[cfg(feature = "proxy")]
            DatabaseConnectionType::ProxyDatabaseConnection(conn) => conn.get_database_backend(),
            DatabaseConnectionType::Disconnected => panic!("Disconnected"),
        }
    }

    /// Creates a [`SchemaBuilder`] for this backend
    pub fn get_schema_builder(&self) -> SchemaBuilder {
        Schema::new(self.get_database_backend()).builder()
    }

    #[cfg(feature = "entity-registry")]
    #[cfg_attr(docsrs, doc(cfg(feature = "entity-registry")))]
    /// Builds a schema for all the entites in the given module
    pub fn get_schema_registry(&self, prefix: &str) -> SchemaBuilder {
        let schema = Schema::new(self.get_database_backend());
        crate::EntityRegistry::build_schema(schema, prefix)
    }

    /// Sets a callback to metric this connection
    pub fn set_metric_callback<F>(&mut self, _callback: F)

View on GitHub (pinned to e29bcd1b41)

Solutions

  1. Ensure connect() succeeded (propagate the Result) before using the connection
  2. Check conn.get_database_backend() only inside code paths reached after a successful connect
  3. Store the backend explicitly at connect time if you need it before queries

Example fix

// before
let conn = Database::connect(&url).await; // Err swallowed
let backend = conn.get_database_backend(); // panics if connect failed
// after
let conn = Database::connect(&url).await?;
let backend = conn.get_database_backend(); // safe
Defensive patterns

Strategy: try-catch

Validate before calling

// propagate connect errors instead of ignoring them:
let conn = Database::connect(&url).await.map_err(|e| anyhow!("db connect failed: {e}"))?;

Type guard

fn connected(conn: &DatabaseConnection) -> bool { std::panic::catch_unwind(|| { let _ = conn.get_database_backend(); }).is_ok() }

Try / catch

// at a boundary, catch panics from downstream library code:
let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| conn.get_database_backend()));
if r.is_err() { /* re-initialize the connection */ }

Prevention

When it happens

Trigger: Calling get_database_backend() (directly or via get_schema_builder, query builders that inspect the backend) on a DatabaseConnection that was never initialized via connect(), or after it was replaced/torn down.

Common situations: Connect() returning Err but the code continuing to use the (disconnected) connection value; using a connection moved out of a struct; errors swallowed with let _ = connect();

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


AI-assisted analysis of SeaQL/sea-orm@e29bcd1b41 (2026-09-10). Data as JSON: /api/errors/7f3085eba2c1760a. Report an issue: GitHub.