1

I'm new to Rust, and I'm having an issue I can't wrap my head around.

My Cargo.toml looks like this:

[dependencies]
rocket = "0.4.6"
rocket_codegen = "0.4.6"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"

[dependencies.diesel]
version = "1.0.0"
default-features = false
features = ["sqlite"]

[dependencies.rocket_contrib]
version = "0.4.6"
default-features = false
features = ["diesel_sqlite_pool"]

[global.databases.urls_db]
url = "urls.sqlite"

I have the connection like this on the main.rs:

use rocket_contrib::databases::diesel;

#[database("urls_db")]
struct UrlsDbConn(diesel::SqliteConnection);

The only table I have is one called urls, as per schema.rs:

table! {
    urls (id) {
        id -> Text,
        uri -> Text,
    }
}

But when I try to cargo run it I get:

Error: Database configuration failure: 'urls_db'
    => Error: A table named `databases` was not found for this configuration

Any idea why?

The only place I have a databases (plural) string is on the Cargo.toml (global.databases.urls_db), which is how rocket.rs instructs to set it up...


EDIT: fixed typo; EDIT2: the database itself seems fine:

main: /Users/carlos/Developer/caarlos0/beckerly/beckerly/urls.sqlite
sqlite> .tables
__diesel_schema_migrations  urls
sqlite> select * from urls;
sqlite> insert into urls(id, uri) values ('g', 'https://google.com');
sqlite> select * from urls;
g|https://google.com
sqlite>
caarlos0
  • 20,020
  • 27
  • 85
  • 160
  • Did you check if database name my_db exists in sqlite – Sankar Boro Jan 11 '21 at 12:24
  • Could it be something needed by Sqlite? – TheCoolDrop Jan 11 '21 at 12:24
  • I think the db should be fine, it ran the migrations just fine (`create table`)... – caarlos0 Jan 11 '21 at 12:28
  • I think the error is referring to "table", as in a section of TOML configuration. Besides, where did the `my_db` string come from? Could it be the discrepancy between names here: `urls_db` and `my_db`? – justinas Jan 11 '21 at 12:29
  • Fixed the `my_db` thing, initially I was hiding the db name in the question and later gave up but forgot to change this part. – caarlos0 Jan 11 '21 at 12:31
  • @justinas Yeah, it also seems to me that it is complaining about something in the toml file, not sure what it is expecting though... – caarlos0 Jan 11 '21 at 12:32
  • 2
    I am not too knowledgeable with Rocket, but shouldn't you use `Rocket.toml` rather than `Cargo.toml` for the database configuration? – justinas Jan 11 '21 at 12:40
  • @justinas exactly, just found that out too (after hours debugging). – caarlos0 Jan 11 '21 at 12:44

1 Answers1

3

The database config should be in a Rocket.toml file:

[global.databases]
urls_db = { url = "urls.sqlite" }

The documentation clearly says so:

Then, in Rocket.toml or the equivalent via environment variables, configure the URL for the database in the databases table:

vallentin
  • 23,478
  • 6
  • 59
  • 81
caarlos0
  • 20,020
  • 27
  • 85
  • 160