Sqlx: Runtime Tokio no works in new project

Created on 21 Nov 2020  路  4Comments  路  Source: launchbadge/sqlx

Hi Guys

I have a new project and i'm tryign use tokio runtime but it not works

I have this error
thread 'main' panicked at 'must be called from the context of Tokio runtime configured with either basic_scheduler or threaded_scheduler

This is my dependencies in Cargo.toml

[dependencies]
sqlx = { version = "0.4.1", features = [ "runtime-tokio-rustls", "postgres" ] }
tokio = { version = "0.3.4", features = ["full"] }

And this is my main.rs

#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {

    // Create a connection pool
    let pool = PgPoolOptions::new()
        .max_connections(5)
        .connect(&env::var("DATABASE_URL").unwrap())
        .await?;

    // Make a simple query to return the given parameter
    let row: (i64,) = sqlx::query_as("SELECT $1")
        .bind(150_i64)
        .fetch_one(&pool)
        .await?;

    assert_eq!(row.0, 150);
    Ok(())
}

If switch runtime-tokio-rustls for runtime-actix-rustls in dependencies and #[tokio::main] for #[actix_web::main] (adding actix web and removing tokio from Cargo.toml) it works but i want use Tokio

This is an issue o need some extra config?

Most helpful comment

Tokio 0.3 isn't compatible with Tokio 0.2 with is used in sqlx.
You can just switch you project to tokio 0.2 or use a compatibility layer but I can not find the doc about that at that moment.

All 4 comments

Tokio 0.3 isn't compatible with Tokio 0.2 with is used in sqlx.
You can just switch you project to tokio 0.2 or use a compatibility layer but I can not find the doc about that at that moment.

sqlx still uses tokio 0.2, you can either change the tokio version you are depending on or use tokio-compat-02.

Thank you very much for your help, I switch to Tokio 0.2 and now works!!

P.S. for newbies like me, maybe it would be nice this would be specified in the repo readme.

For reference, a 0.3<->0.2 compat crate is here: https://crates.io/crates/tokio-compat-02.

Your code above would look something like:

use tokio_compat_02::FutureExt;

...

    let pool = PgPoolOptions::new()
        .max_connections(5)
        .connect(&env::var("DATABASE_URL").unwrap())
        .compat()  // <----------
        .await?;
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ecton picture ecton  路  5Comments

PoiScript picture PoiScript  路  5Comments

liangyongrui picture liangyongrui  路  3Comments

kunjee17 picture kunjee17  路  3Comments

c410-f3r picture c410-f3r  路  8Comments