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
1
vote
2 answers
error: to use a constant of type `TypeId` in a pattern, `TypeId` must be annotated with `#[derive(PartialEq, Eq)]`
I am a newbie and having an issues with my program. Trying to write a simple backend program which communicates with mqtt broker and does some crude operations into database.
I am getting the compiler error below and I honestly not sure what it is…

tonicChord
- 11
- 4
1
vote
2 answers
How can I pass a mutable reference through a function pointer when the inner mutable has a shorter lifetime?
I am trying to write a function which takes a pointer to a function which executes some SQL query nested in a transaction. I have been running into errors with this for a day now and I do not know how to solve it.
Minimum Reproducible Example
Rust…

Bryan Reilly
- 114
- 4
1
vote
1 answer
How do i create custom Postgres enum types in Rust SQLx?
I am trying to create a custom enum type in Postgres and have done so successfully. My migration looks like this:
CREATE TYPE role AS ENUM ('admin', 'user');
ALTER TABLE users
ADD role role DEFAULT 'user';
Then i have created the enum type in Rust…

Kanerix
- 11
- 2
1
vote
0 answers
How safe is sqlx compile time checks?
If I understand correctly, it is possible for sqlx to do a form of compile time verification of queries. And the way it does this, is at compile time, it connects to the database using DATABASE_URL env variable and performs some checks against the…

Finlay Weber
- 2,989
- 3
- 17
- 37
1
vote
1 answer
Rust create PrimitiveDateTime with now as value and time as crate
I am using the crate time, and I want to create a UTC datetime with now as reference (like "take machine time"). I can do that with OffsetDateTime and now_utc() constructor. but I want a primitive datetime cause I use sqlx and the database type is…

Swann HERRERA
- 37
- 5
1
vote
1 answer
error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query_as`
I want to send query to database, but I faced this error:
this error originates in the macro $crate::sqlx_macros::expand_query which comes from the expansion of the macro sqlx::query_as (in Nightly builds, run with -Z macro-backtrace for more…

Mr Coder
- 761
- 2
- 13
- 34
1
vote
1 answer
How to access database connection from a Rocket's fairing?
I'm trying to use database connection from a Rocket's on_ignite fairing:
use sqlx::{ self, FromRow };
use rocket::fairing::{self, Fairing, Info, Kind};
use rocket::{Build, Rocket};
use crate::database::PostgresDb;
#[derive(FromRow)]
struct…

SinxroFozotron
- 48
- 6
1
vote
1 answer
Inserting records into MySQL via sqlx gets very slow at some point
Using:
sqlx = { version = "0.6.2", features = ["runtime-tokio-native-tls", "mysql"] }
When I run the following code (in release mode) against a MySQL 8.0.31 server running on the same machine (no AUTO_COMMIT):
let mut tx = pool.begin().await?;
for…

at54321
- 8,726
- 26
- 46
1
vote
1 answer
When building a docker container using rust, how can I build `actix-web` & `sqlx` without throwing `warning: spurious network error`s?
BACKGROUND:
I'm trying to build a postgres-compatible rust server using actix-web & sqlx into a docker container. It's a basic RESTful API for a mock blog, just so I can establish a basic scaffolding for a useful server architecture that I can use…

myssyng lynx
- 11
- 2
1
vote
0 answers
1
vote
1 answer
Handling lifetimes when decoding generic types using SQLx
I am attempting to use a generic method to retrieve values from database tables that are structurally identical, but have different types for one of their columns. Simplified example below
async fn query<'a, 'r, T: DatabaseType- + Decode<'r,…

tamathews01
- 47
- 6
1
vote
0 answers
How to share a transaction between repositories?
I use sqlx to communicate with my Postgres database. I'm trying to abstract the database communication with a Repository pattern. Aslo, with this abstraction I would like to share the database transaction between repositories using Unit Of Work…

Roman Mahotskyi
- 4,576
- 5
- 35
- 68
1
vote
0 answers
Rust sqlx: where any(enum)
I'm trying to select on a subset of enum variants, but getting the trait PgHasArrayType is not implemented. Any suggestions?
use sqlx::Row;
#[derive(Debug, sqlx::Type)]
#[sqlx(type_name = "color")]
#[sqlx(rename_all = "lowercase")]
enum Color {
…

imbolc
- 1,620
- 1
- 19
- 32
1
vote
1 answer
How to represent array of strings column in Rust sqlx
I have a table called product; one of the columns called tags is JSONB type and essentially contains an array of strings. Each product in the table has various amounts of tags. Some can be 0 some can be 40. I've created a struct in Rust to best…

G.Rose
- 644
- 7
- 29
1
vote
1 answer
sqlx: implementing Decode and Type for associated trait type
I am trying to create a library which gets Values for multiple Tags from an SQL Database. Depending on the TagType which can be Analog or String - I need to return the matching value type. I tried to achieve this using a associated type named…

he4d
- 13
- 3