Search code examples
rustrust-rocket

How to debug a "missing field `url`" error with the rocket_db_pools library


I have a Postgres database running at the following url

postgresql://postgres:postgres@localhost:5432/my_db

I am able to connect to it using pgAdmin and psql

psql postgresql://postgres:postgres@localhost:5432/my_db

I have the following Cargo.toml file

[dependencies]
rocket = { version = "0.5.0-rc.3", features = ["json"] }

[dependencies.rocket_db_pools]
version = "=0.1.0-rc.3"
features = ["sqlx_postgres"]

[default.databases.my_db_name]
url = "postgresql://postgres:postgres@localhost:5432/my_db"

and this minimal rocket server

#[macro_use]
extern crate rocket;

use rocket::serde::json::Json;
use rocket_db_pools::{sqlx, Connection, Database};

#[derive(Database)]
#[database("my_db_name")]
struct MyDB(sqlx::PgPool);

#[get("/test")]
async fn test(
    mut _db: Connection<MyDB>,
) -> Result<Json<()>, Json<()>> {
    Ok(Json(()))
}

#[launch]
fn rocket() -> _ {
    rocket::build()
        .mount("/", routes![test]) 
        .attach(MyDB::init())  
}

It feels like this exemple follows the documentation, but when running it I get the following

Error: failed to initialize database: bad configuration: missing field `url`
Error: Rocket failed to launch due to failing fairings:
   >> 'my_db' Database Pool
thread 'main' panicked at 'aborting due to fairing failure(s)', 
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

How can I know why isn't rocket_db_pools finding the URL field ?


Solution

  • You have to provide the configuartion for rocket in a file called Rocket.toml, not in the Cargo.toml as the docs state:

    By default, configuration can be provided in Rocket.toml