SeaQL/sea-orm · critical
Disconnected
Error message
Disconnected
What it means
This panic fires when `get_database_backend()` (or a similar backend-inspecting method) is called on a `DatabaseConnection` whose inner `DatabaseConnectionType` is the `Disconnected` variant. SeaORM represents an un-established, closed, or never-connected connection this way, and there is no backend to report for a connection that was never opened. It is a hard panic, not a recoverable `Result`, because the API contract assumes a live connection.
Source
Thrown at src/database/db_connection.rs:727
///
/// # 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
- Ensure `DatabaseConnection::connect(...)` is awaited and its `Result` is propagated before any query or backend query; never use `.unwrap_or_default()` on connect.
- Check `conn.is_valid()` / match on `DatabaseConnectionType` before calling backend-dependent methods.
- Initialize the connection in application setup (e.g. actix `web::Data`) so handlers only ever receive a connected handle.
- If lazily initializing, gate access behind an `Option<DatabaseConnection>` or `OnceCell` so a disconnected handle can't be reached.
Example fix
// before
let db: DatabaseConnection = DatabaseConnection::default();
let backend = db.get_database_backend(); // panics: Disconnected
// after
let db = Database::connect("sqlite://db.sqlite?mode=rwc").await?;
let backend = db.get_database_backend(); // safe: connection is live Defensive patterns
Strategy: validation
Validate before calling
if !conn.is_valid() || matches!(conn, DatabaseConnection { inner: DatabaseConnectionType::Disconnected, .. }) {
return Err(anyhow!("database connection not established"));
} Type guard
fn is_connected(conn: &DatabaseConnection) -> bool {
!matches!(conn.as_ref(), DatabaseConnectionType::Disconnected)
} Prevention
- Always propagate the Result of Database::connect; never unwrap_or_default.
- Store the connection in app state only after a successful connect.
- Call is_valid()/ping before first use in long-running processes.
- Wrap lazy connections in Option/OnceCell so a disconnected handle is unreachable.
When it happens
Trigger: Calling `get_database_backend()`, `get_schema_builder()`, or any backend-querying method on a `DatabaseConnection` created via `DatabaseConnection::default()` / `Disconnected`, or after the connection was explicitly disconnected/replaced with a disconnected handle.
Common situations: Declaring a `DatabaseConnection::default()` struct field and using it before `connect()` succeeds; storing the connection after `connect()` returned an `Err` but the error was swallowed with `.unwrap_or_default()`; using a connection whose pool was shut down in tests.
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/26fe17690961ea72.
Report an issue: GitHub.