Hello everyone,
I'm currently working on a Discord bot using Rust and have encountered an issue with the sqlx crate, specifically with the sqlx::query! macro. Despite setting the DATABASE_URL in my .env file, I'm still facing an error related to this macro. Here's a brief overview of my situation:
Issue:
When compiling my bot, I receive the following error:
error: set `DATABASE_URL` to use query macros online, or run `cargo sqlx prepare` to update the query cache
--> src\main.rs:41:9
|
41 | / sqlx::query!(
42 | | "INSERT INTO user_xp (user_id, xp, level) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE xp = xp + ?, level = ?",
43 | | user_id,
44 | | xp_gain,
... |
47 | | new_level,
48 | | )
| |_________^
What I've Tried:
- Ensuring the
.envfile is correctly placed and formatted with theDATABASE_URLenvironment variable. - Directly embedding the database URL into the code (for troubleshooting purposes).
- Running
cargo sqlx prepareto update the query cache, but recieved the following warning:
error: no such command: `sqlx`
Did you mean `fix`?
View all installed commands with `cargo --list`
Despite these attempts, the error persists. The strange part is that another environment variable (DISCORD_TOKEN) is being read correctly from the same .env file.
Code Context:
Here's a snippet of the code where the error occurs:
sqlx::query!(
"INSERT INTO user_xp (user_id, xp, level) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE xp = xp + ?, level = ?",
user_id,
xp_gain,
new_level,
xp_gain,
new_level,
)
.execute(pool)
.await
.unwrap();
Environment Details:
- Rust version: rustc 1.74.0 (79e9716c9 2023-11-13)
- Database: MySQL
Dependencies: - serenity = { version = "0.12.0", features = ["full"] }
- tokio = { version = "1", features = ["full"] }
- rand = "0.8.4"
- sqlx = { version = "0.7", features = ["mysql", "runtime-tokio", "tls-rustls"] }
- dotenv = "0.15.0"
I would greatly appreciate any insights or suggestions on how to resolve this issue. It's puzzling why the DATABASE_URL isn't being recognized for the sqlx::query! macro, despite seemingly correct setup and other environment variables working fine, maybe I'm just overlooking something dumb.
My full code can be found on my GitHub page here.
Thank you in advance for your help!