错误源自宏 `$crate::sqlx_macros::expand_query`,它来自宏 `sqlx::query_as` 的展开。

huangapple go评论79阅读模式
英文:

error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query_as`

问题

我想发送查询到数据库,但我遇到了这个错误:

> 此错误源于宏 $crate::sqlx_macros::expand_query,它来自宏 sqlx::query_as 的展开(在 Nightly 构建中,使用 -Z macro-backtrace 以获取更多信息)

> 必须设置 DATABASE_URL 以使用查询宏

我正在使用 Rocket 框架和 PostgreSQL。

代码:

#[get("/user/<uuid>", rank = 1, format = "text/plain")]
async fn user(pool: &rocket::State<PgPool>, uuid: &str) -> Result<User, Status> {
    let parsed_uuid = Uuid::parse_str(uuid).map_err(|_| Status::BadRequest)?;

    let user = sqlx::query_as!(User, "SELECT * FROM users WHERE uuid = $1", parsed_uuid)
        .fetch_one(pool.inner())
        .await;
    user.map_err(|_| Status::NotFound)
}

#[launch]
async fn rocket() -> Rocket<Build> {
    let our_rocket = rocket::build();

    let config: Config = our_rocket
        .figment()
        .extract()
        .expect("Incorrect Rocket.toml configuration");

    let pool = PgPoolOptions::new()
        .max_connections(5)
        .connect(&config.database_url)
        .await
        .expect("Failed to connect to the database");

    our_rocket.manage(pool)
}
[dependencies]
rocket = { version = "0.5.0-rc.2", features = ["secrets"] }
sqlx = { version = "0.6.2", features = ["postgres","uuid", "runtime-tokio-rustls"] }

如何解决这个问题?

英文:

I want to send query to database, but I faced this error:

> this error originates in the macro $crate::sqlx_macros::expand_query which comes from the expansion of the macro sqlx::query_as (in Nightly builds, run with -Z macro-backtrace for more info)

>DATABASE_URL must be set to use query macros

I am using the Rocket framework and PostgreSQL.

Code:

#[get(&quot;/user/&lt;uuid&gt;&quot;, rank = 1, format = &quot;text/plain&quot;)]
async fn user(pool: &amp;rocket::State&lt;PgPool&gt;, uuid: &amp;str) -&gt; Result&lt;User, Status&gt; {
    let parsed_uuid = Uuid::parse_str(uuid).map_err(|_| Status::BadRequest)?;
    
    let user = sqlx::query_as!(User, &quot;SELECT * FROM users WHERE uuid = $1&quot;, parsed_uuid)
        .fetch_one(pool.inner())
        .await;
    user.map_err(|_| Status::NotFound)
}

#[launch]
async fn rocket() -&gt; Rocket&lt;Build&gt; {
    let our_rocket = rocket::build();
    
    let config: Config = our_rocket
        .figment()
        .extract()
        .expect(&quot;Incorrect Rocket.toml configuration&quot;);

    let pool = PgPoolOptions::new()
        .max_connections(5)
        .connect(&amp;config.database_url)
        .await
        .expect(&quot;Failed to connect to database&quot;);
    
    our_rocket.manage(pool)
}
[dependencies]
rocket = { version = &quot;0.5.0-rc.2&quot;, features = [&quot;secrets&quot;] }
sqlx = {version = &quot;0.6.2&quot;, features = [&quot;postgres&quot;,&quot;uuid&quot;, &quot;runtime-tokio-rustls&quot;]}

How can i solve that problem?

答案1

得分: 1

sqlx 使用数据库来检查您的查询类型是否正确。要使用它的宏,您需要定义环境变量 DATABASE_URL,使用一个 .env 文件或提供一个 sqlx-data.json 文件。您可以将 DATABASE_URL 设置为带有所需模式的数据库,然后可以运行适当的环境设置的 cargo 命令,例如 DATABASE_URL="postgres://your_database_info" cargo (run|build|check|...)

英文:

sqlx uses the database to check if your queries types are correct, to use it's macros you have to define the environment variable DATABASE_URL, use a .env file or provide a sqlx-data.json you can set DATABASE_URL to a database with the schema required, you can run cargo with the appropriate environment setup: DATABASE_URL=&quot;postgres://your_database_info&quot; cargo (run|build|check|…)

huangapple
  • 本文由 发表于 2023年2月18日 16:17:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定