英文:
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("/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 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"]}
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="postgres://your_database_info" cargo (run|build|check|…)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论