The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, SQLite, and MSSQL. Don't confuse with the sqlx tag, which is a GO library.
Questions tagged [rust-sqlx]
145 questions
5
votes
0 answers
How do I run nested queries in sqlx rust?
I want to run a nested query in sqlx. Here's what I've tried:
sqlx::query_as!(NseData, "select * from nse_data where trading_day = (select max(trading_day) from nse_data)").fetch_one(&app_context.db_connection).await?;
But it gives me the…

Asnim P Ansari
- 1,932
- 1
- 18
- 41
4
votes
1 answer
cargo sqlx prepare produces stub file -- even with 'offline' feature enabled
I am using some sqlx::query! and sqlx::query_as! and my project compiles fine. But when I run cargo sqlx prepare I get first and error:
$ cargo sqlx prepare
error: extra arguments to `rustc` can only be passed to one target, consider filtering
the…

ryan
- 51
- 4
4
votes
1 answer
How to dynamically insert a column name into the query! macro in Rust's sqlx
I'm using the sqlx crate to interact with a Postgres instance.
I have the following function:
pub async fn fetch_tweets_by_metric(
pool: &PgPool,
metric: &str,
) -> Result, sqlx::error::Error> {
let tweets = sqlx::query_as!(
…

ilmoi
- 1,994
- 2
- 21
- 45
4
votes
1 answer
How can I make a postgres pool connection global in rocket.rs and make FromRequest custom guards asyncable?
I am making an authorization system which will implement the FromRequest trait from the web framework rocket, which is needed to make custom guards.
The first problem I have gone into is how I have to make the connection global. Should I cast it as…

tsu_katana
- 47
- 1
- 5
3
votes
0 answers
How to pretty print the DB statement on debug level?
I'm trying to understand how can I "pretty" format the below traced DB statement sqlx gives me using tracing:
2023-08-01T15:54:42.675396Z DEBUG sqlx::query:117: summary: "SELECT \"user\".* FROM \"user\" …", db.statement: "\n\nSELECT\n …

Fred Hors
- 3,258
- 3
- 25
- 71
3
votes
1 answer
Inserting a struct into an sqlite db using sqlx and rust
I have a question regarding inserting a data structure into a db and I can't seem to find any documentation on.
I have a data structure
#[derive(FromRow, Getters, Default, Serialize, Deserialize, Debug)]
#[serde(crate =…

RuboGubo
- 35
- 5
3
votes
1 answer
Running rust sqlx migrations locally with docker-compose
I'm working through Zero to Prod in Rust and I've gone off script a bit. I'm working on dockerizing the whole setup locally including the database. On ENTRYPOINT the container calls a startup script that attempts to call sqlx migrate run, leading to…

BWStearns
- 2,567
- 2
- 19
- 33
3
votes
1 answer
How can I instruct sqlx about this custom type I'm using for OffsetDateTime?
I have a custom time type because time's authors doesn't want to add a Default value yet:
#[derive(Debug, Clone, Copy)]
pub struct OffsetDateTime(pub time::OffsetDateTime);
impl OffsetDateTime {
pub fn now_utc() -> Self {
…

Fred Hors
- 3,258
- 3
- 25
- 71
3
votes
1 answer
sqlx: Unsupported type _badge of column #6 ("badges")
I'm using Rust's sqlx crate, and trying to perform the following:
query_as!(User, r#"SELECT * FROM users WHERE name ILIKE $1"#, name)
Note that I am using Postgresql.
I am confronted with this error:
error: unsupported type _badge of column #6…

ImajinDevon
- 287
- 1
- 11
3
votes
0 answers
Rust: Convert SQL types to Rust automatically using sqlx
I'm new to rust and was working on a generic database viewer app using SQLX. Now I've stumbled upon a problem. While querying like a simple
SELECT * FROM TABLE_NAME
Now the problem is that I don't know the type of rows and columns before hand. So…

Ayan Banerjee
- 151
- 1
- 3
- 11
3
votes
2 answers
Drop database on drop Using Sqlx and Rust
I'm following step by step the zero2prod Rust book. So far, I'm in the last parts of Chapter 3 in which we setup some integration tests and create a database per test run.
I'd like to extend this functionality and add the capability to drop the…

Jose A
- 10,053
- 11
- 75
- 108
3
votes
1 answer
Convert PgRow value of unknown type to a String
I'm trying to "pretty-print" the result of a query. I don't know the query beforehand, so I can't manually convert. My idea was to iterate over the column information for the PgRow, and then for each column get the value.
This is the code I have…

CircuitSacul
- 1,594
- 12
- 32
3
votes
0 answers
Rust Sqlx Custom implementation FromRow mapping
Need:
I am trying to implement sql:: frowrow, but an error is reported when defining it
in fn new(pool: Arc)
_from_row parameter error
pub struct Table<'c, T>
where
T: FromRow<'c, MySqlRow>,
{
pub pool: Arc,
_from_row:…

zebei li
- 31
- 3
3
votes
1 answer
getting row data from sql::query_as
Im trying to use sqlx to get data from a mysql db. I have the following:
#[derive(Debug, PartialEq, Eq, sqlx::FromRow)]
struct Room {
name: String
}
let mut stream = sqlx::query_as::<_, Room>(r#"SELECT name FROM rooms"#)
…

discodowney
- 1,475
- 6
- 28
- 58
3
votes
0 answers
How to solve "implementation of `sqlx::Acquire` is not general enough"
I'm creating a actix_web app.
I tried to spawn a new task and use functions, which are already in use in actix_web part.
But if I compile, I see following error with some life-time information.
To be honest, I've no idea what to do with this…

macalloy
- 43
- 7