Sqlx query macro

Hi guys, I had some problems using sqlx, here is the code.

let res = sqlx::query!(r#"SELECT Version();"#).fetch_all(db).await?;

and it says something that column name "Version()" is invalid: "Version()" is not a valid Rust identifier.

thanks.

The query! macro will create an anonymous struct using the column names from your query as the field names in this struct. So the error says you can't use Version() as a field name in this struct. You can get a valid identifier if your query looks more like this:

let res = sqlx::query!(r#"SELECT Version() as version"#).fetch_all(db).await?;

Hi thank you very much, but I came to another problem
there is no reactor running, must be called from the context of a Tokio 1.x runtime
I don't know why, but I wrote in actix_web::main function. could u also help me? thanks

I don't have experience with actix, but it appears to be running on an older version of tokio not compatible with sqlx. Here's a SO answer that might help, which suggests using a 4.0.0-beta.x version of actix.

1 Like

Yeah, I believe this is because your sqlx version and actix-web version don't "match" in terms of the tokio runtimes they use under the hood. I had to recently solve this problem in a personal project fo mine and I've found that these are the compatible version pairings for both libs:

  • sqlx @ v0.4 & actix-web @ v3.3 or
  • sqlx @ v0.5 & actix-web @ v4 (currently in beta)