sqlx
sqlx copied to clipboard
Quickstart with SQLite yields error: relative URL without a base
I'm trying to follow the quickstart with SQLite:
// in .env, DATABASE_URL=/absolute/path/to/database.db
struct ResultStruct;
async fn reproduce_bug() -> Result<(), sqlx::Error> {
let pool = SqlitePoolOptions::new().max_connections(5).connect("/absolute/path/to/database.db").await;
sqlx::query_as!(
ResultStruct,
"
SELECT *
FROM table
"
)
.fetch_all(&pool)
.await?;
Ok(())
}
This fails to build with:
error: relative URL without a base
...
= note: this error originates in the macro `$crate::sqlx_macros::expand_query` (in Nightly builds, run with -Z macro-backtrace for more info)
- SQLx: 0.5.13
- SQLite3: 3.31.1
- libsqlite3-sys: 0.24.2
Using sqlite3 directly, I'm able to open the database as expected, so I'm confident it's not a copy/paste error in the path.
It looks like maybe SQLx is expecting the URL to be prefixed with "sqlite://"? What are the various connections strings for the supported databases? Is this documented somewhere?
E.g. DATABASE_URL=sqlite://db/production.db
(where db/production.db
is the relative path to my SQLite database) looks to get me different errors that seem further along.
Found the documentation here: https://docs.rs/sqlx/latest/sqlx/sqlite/struct.SqliteConnectOptions.html.
The SQLx docs state:
A value of SqliteConnectOptions can be parsed from a connection URI, as described by SQLite.
The SQLite docs state:
SQLite uses the "file:" URI syntax to identify database files.
This is some of where my confusion arose. Is this a potentially misleading doc (difference between the file:
scheme SQLite expects and the sqlite:
scheme SQLx expects), or SQLx bug/design decision?
run this and try again:
export DATABASE_URL="{db_protocol}://{user}:{password}@{host}:{port}/{database}"
in my case, inside a docker compose cluster with dns resolution:
export DATABASE_URL="postgres://admin:admin@database:5432/ssleo"
Superceded by #3099