sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

Runtime Tokio no works in new project

Open neo-clon opened this issue 6 years ago • 4 comments

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?

neo-clon avatar Nov 21 '20 19:11 neo-clon

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.

dragonnn avatar Nov 21 '20 19:11 dragonnn

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

raftario avatar Nov 21 '20 19:11 raftario

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.

neo-clon avatar Nov 21 '20 19:11 neo-clon

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?;

NicholasGorski avatar Dec 19 '20 10:12 NicholasGorski