SeaQL/sea-orm · error

Not MySQL Connection

Error message

Not MySQL Connection

What it means

`get_mysql_connection_pool()` extracts the raw `sqlx::MySqlPool` from a SeaORM connection, but only when the inner variant is `SqlxMySqlPoolConnection`. Any other backend (Postgres, SQLite, mock, disconnected) hits the catch-all arm and panics. The docs explicitly state it panics if the connection is not MySQL.

Source

Thrown at src/database/db_connection.rs:831

                // Nothing to cleanup, we just consume the `DatabaseConnection`
                Ok(())
            }
            DatabaseConnectionType::Disconnected => Err(conn_err("Disconnected")),
        }
    }
}

impl DatabaseConnection {
    /// Get [sqlx::MySqlPool]
    ///
    /// # Panics
    ///
    /// Panics if [DbConn] is not a MySQL connection.
    #[cfg(feature = "sqlx-mysql")]
    pub fn get_mysql_connection_pool(&self) -> &sqlx::MySqlPool {
        match &self.inner {
            DatabaseConnectionType::SqlxMySqlPoolConnection(conn) => &conn.pool,
            _ => panic!("Not MySQL Connection"),
        }
    }

    /// Get [sqlx::PgPool]
    ///
    /// # Panics
    ///
    /// Panics if [DbConn] is not a Postgres connection.
    #[cfg(feature = "sqlx-postgres")]
    pub fn get_postgres_connection_pool(&self) -> &sqlx::PgPool {
        match &self.inner {
            DatabaseConnectionType::SqlxPostgresPoolConnection(conn) => &conn.pool,
            _ => panic!("Not Postgres Connection"),
        }
    }

    /// Get [sqlx::SqlitePool]
    ///

View on GitHub (pinned to e29bcd1b41)

Solutions

  1. Verify the connection backend first: only call `get_mysql_connection_pool()` when `db.get_database_backend() == DbBackend::MySql`.
  2. Check the database URI in configuration points to MySQL (`mysql://`), not another engine.
  3. Use `match` on the connection or a `try_get_*` style accessor instead of the panicking getter in generic code.
  4. In tests, configure the mock database only where the mysql-specific path is not exercised.

Example fix

// before
let pool = db.get_mysql_connection_pool(); // panics if backend isn't MySQL

// after
assert!(matches!(db.get_database_backend(), DbBackend::MySql), "expected MySQL connection");
let pool = db.get_mysql_connection_pool();
Defensive patterns

Strategy: type-guard

Validate before calling

if conn.get_database_backend() != DbBackend::MySql {
    return Err(anyhow!("get_mysql_connection_pool requires a MySQL backend"));
}

Type guard

fn as_mysql_pool(conn: &DatabaseConnection) -> Option<&sqlx::MySqlPool> {
    (conn.get_database_backend() == DbBackend::MySql)
        .then(|| conn.get_mysql_connection_pool())
}

Try / catch

std::panic::catch_unwind(|| conn.get_mysql_connection_pool()) // last resort; prefer backend check

Prevention

When it happens

Trigger: Calling `db.get_mysql_connection_pool()` on a connection established from a `postgres://`, `sqlite://` URI, a `MockDatabase`, or a disconnected handle.

Common situations: Code shared across backends that unconditionally pulls the MySQL pool; config/env pointing at the wrong database URL; running integration tests against the mock backend; compile-time feature mismatch (`sqlx-mysql` enabled but app connects to Postgres).

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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