sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

Quickstart with SQLite yields error: relative URL without a base

Open tobymurray opened this issue 2 years ago • 2 comments

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.

tobymurray avatar Apr 15 '22 20:04 tobymurray

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.

tobymurray avatar Apr 15 '22 20:04 tobymurray

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?

tobymurray avatar Apr 17 '22 16:04 tobymurray

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"

leoleitedev avatar Feb 27 '24 04:02 leoleitedev

Superceded by #3099

abonander avatar Mar 06 '24 09:03 abonander