Repository navigation
Description
It would be very helpful to get more examples that show the features and possibilities of this library. It would help tremendously to set boundaries of what to expect for newcomers. This could help to mitigate the currently missing guide. As a newcomer it is very frustrating to get to a starting point or even validate if this library is a good fit for what one is searching for. Yes the documentation is quite good but it is only useful for people that are familiar with the project and know what to look for. Chasing types might also be to hard in the beginning to interpret certain error messages.
If you want to achieve something – and thus having an expectation – it is very hard to
- find information if it is possible what you want to achieve
- find information how to achieve it if you think it is possible
To give a concrete example and use case. As a newcomer i want to create an application that manages accounts, addresses and posts written from these accounts. So that is your common one to one and one to many relationship. One account has a single address and has many posts. How do i achieve that with SQLx? Digging through the documentation and the sparse examples you learn about query_as and sqlx::Type and end up with a starting point that has you covered with accounts and addresses
create table address (
address_id BIGSERIAL primary key,
street text not null,
street_number text not null,
city text not null,
country text not null
);
create table accounts (
account_id BIGSERIAL primary key,
user_name text not null,
address_id BIGINT,
constraint fk_address foreign key(address_id) references address(address_id)
);#[derive(Debug, sqlx::Type)]
struct Address {
address_id: i64,
street: String,
street_number: String,
city: String,
country: String,
}
#[derive(Debug)]
struct AccountNoPost {
account_id: i64,
user_name: String,
address: Address,
}
async fn get_account_with_address(pool: &PgPool) -> Result<(), sqlx::Error> {
let account = sqlx::query_as!(AccountNoPost, r#"
select
ac.account_id, ac.user_name,
(ad.address_id, ad.city, ad.country, ad.street, ad.street_number) as "address!: Address"
from accounts as ac
join address as ad using(address_id)
where ac.account_id = 1;
"#).fetch_one(pool).await?;
println!("account: {:#?}", account);
Ok(())
}That alone could be one example that could save a tremendous amount of time for a newcomer. It shows what is possible and how to achieve it. Sprinkled with a little more explanatory text and hints to the specific places in the documentation that explains it more in detail could act as a starting point for a future guide.
Wanting to have your one to many case covered is a hole other deal. Now the documentation gets hard to understand and there are no examples. Looking through various issues ( #856, #714, #298, #552, ... etc ) only gets you more confused. So is it possible to get your data in a single query or not? All the issues that are closed have no conclusion or a definitive answer to it, they seem to just derail into details that are unintelligible by newcomers. So was issue #856 just a misplaced comma? Lets try this out
create table posts (
post_id BIGSERIAL primary key,
account_id BIGINT,
title text not null,
body text not null,
constraint fk_account foreign KEY(account_id) references accounts(account_id)
);#[derive(Debug)]
struct Account {
account_id: i64,
user_name: String,
address: Address,
posts: Vec<Post>,
}
#[derive(Debug, sqlx::Type)]
struct Post {
post_id: i64,
title: String,
body: String,
}
async fn get_account_with_address_posts(pool: &PgPool) -> Result<(), sqlx::Error> {
let account = sqlx::query_as!(Account, r#"
select
ac.account_id, ac.user_name,
(ad.address_id, ad.city, ad.country, ad.street, ad.street_number) as "address!: Address",
(p.post_id, p.title, p.body) as "posts!: Vec<Post>"
from accounts as ac
join address as ad using(address_id)
join posts as p using(account_id)
where ac.account_id = 1;
"#).fetch_one(pool).await?;
println!("account: {:#?}", account);
Ok(())
}Ok, that looks like it does not work. Looking at the output that this query is making it looks like that this should not work anyway. Running this query in your sql tool of choice gives you
| account_id | user_name | address!: Address | posts!: Vec |
|---|---|---|---|
| 1 | peter | (1,city_1,country_1,street_1,1a) | (1,"first post","my first post has not much text") |
| 1 | peter | (1,city_1,country_1,street_1,1a) | (2,"second post","my second post has more text, but not much more.") |
| 1 | peter | (1,city_1,country_1,street_1,1a) | (3,"third post","third one is even shorter") |
This looks absolutely wrong but isn't even mentioned in issue #856 . So for this to work it looks like we have to get the posts in a single row.
We can validate that this is working to some extend we can change
#[derive(Debug)]
struct Account {
account_id: i64,
user_name: String,
address: Address,
posts: Post, // <- No Vec<Post>
}
async fn get_account_with_address_posts(pool: &PgPool) -> Result<(), sqlx::Error> {
let account = sqlx::query_as!(Account, r#"
select
ac.account_id, ac.user_name,
(ad.address_id, ad.city, ad.country, ad.street, ad.street_number) as "address!: Address",
(p.post_id, p.title, p.body) as "posts!: Post" -- ## !! No Vec<Post> !! ##
from accounts as ac
join address as ad using(address_id)
join posts as p using(account_id)
where ac.account_id = 1;
"#).fetch_one(pool).await?;
println!("account: {:#?}", account);
Ok(())
}To get just a single Post this looks like it is working.
Changing the query to
select
ac.account_id, ac.user_name,
(ad.address_id, ad.city, ad.country, ad.street, ad.street_number) as "address!: Address",
array_agg((p.post_id, p.title, p.body)) as "posts!: Vec<Post>"
from accounts as ac
join address as ad using(address_id)
join posts as p using(account_id)
where ac.account_id = 1
group by ac.account_id, "address!: Address";should do the trick and gives us
| account_id | user_name | address!: Address | posts!: Vec |
|---|---|---|---|
| 1 | peter | (1,city_1,country_1,street_1,1a) | {"(1,"first post","my first post has not much text")","(2,"second post","my second post has more text, but not much more.")","(3,"third post","third one is even shorter")"} |
however
#[derive(Debug)]
struct Account {
account_id: i64,
user_name: String,
address: Address,
posts: Vec<Post>,
}
async fn get_account_with_address_posts(pool: &PgPool) -> Result<(), sqlx::Error> {
let account = sqlx::query_as!(Account, r#"
select
ac.account_id, ac.user_name,
(ad.address_id, ad.city, ad.country, ad.street, ad.street_number) as "address!: Address",
array_agg((p.post_id, p.title, p.body)) as "posts!: Vec<Post>"
from accounts as ac
join address as ad using(address_id)
join posts as p using(account_id)
where ac.account_id = 1;
"#).fetch_one(pool).await?;
println!("account: {:#?}", account);
Ok(())
}Is not getting us anywhere.
error[E0277]: the trait bound `Vec<Post>: Type<Postgres>` is not satisfied
--> src/main.rs:86:19
|
86 | let account = sqlx::query_as!(Account, r#"
| ___________________^
87 | | select
88 | | ac.account_id, ac.user_name,
89 | | (ad.address_id, ad.city, ad.country, ad.street, ad.street_number) as "address!: Address",
... |
94 | | where ac.account_id = 1;
95 | | "#).fetch_one(pool).await?;
| |_______^ the trait `Type<Postgres>` is not implemented for `Vec<Post>`
|
= help: the following implementations were found:
<Vec<&[u8]> as Type<Postgres>>
<Vec<&str> as Type<Postgres>>
<Vec<(T1, T2)> as Type<Postgres>>
<Vec<(T1, T2, T3)> as Type<Postgres>>
and 22 others
= note: required because of the requirements on the impl of `sqlx::Decode<'_, Postgres>` for `Vec<Post>`
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to previous error
And i don't really understand what the error message is trying to say to me. After digging a little bit further i found this issue #349 where i also don't understand if this is related or not and what the conclusion here is.
At this point i am into my third weekend playing around with sqlx and i don't even know how to use it for a simple one to many use case. This is very frustrating as stated before. sqlx might have many cool features but i just can't get to know them. What is the "idiomatic" way to do such a simple task in sqlx? Am i supposed to do it with multiple different queries as mentioned in this issue #552 ? Is what i was trying to do in the first place even possible with sqlx and i am just not able to express it the right way or is it a limitation/boundary that sqlx has due to the way it is working (which i don't know as a beginner). I suspect issue #298 to be the current roadblocker but i just don't know.
Having a variety of "simple" examples that show how to deal with them in sqlx could save a huge amount of time and frustration for people that just "wanted to take a look" at the project and it could make on boarding significantly easier as the only real way is to search the issues which is often not really helpful. Examples could show current limitations like
this is how you need to make it currently due to issue xyz – we seek to have a solution that would look like this but currently not available.
And the discord channel is not the right tool to search for answers. I can imagine that this sort of questions are asked many times before and is frustrating to answer.
I hope this gets a little insight on my process and experience. Love the project! ❤️
Activity
mehcode commentedon Jan 24, 2021
That is all completely fair. I agree that we need to make this a priority. I think a potentially good solution here is to take a public SQL dataset, and setup several guided examples using that dataset.
Postgres
We can plan to get a mdbook or similar setup with a lot of this and then iterate on it. Is there anything large I'm missing from the above?
pythoneer commentedon Jan 24, 2021
This looks like a very helpful list of examples. I don't know if its better suited for a "tips and tricks" section but i often like to use some kind of
upsertINSERT ON CONFLICTbut this may be to specific to my usual use cases and is not applicable to a wide variety of people and could be easily derived from the current list of examples as it is more or less just another query case we have already covered.A Transactions-Example could be useful? Maybe one that recovers from a
40001by repeating the tx ... may be to specific again.Do you plan to have this as examples and a corresponding mdbook (guide)?
mehcode commentedon Jan 24, 2021
Maybe a first step would be just examples. Trying to reign myself in so this gets done.
I only show mysql above but the idea is we would have
guide/postgres,guide/sqlite, etc.I'm thinking that the larger examples should probably move to another repo like
sqlx-rs/examples.Definitely. At least something like "Start a transaction, insert into two different tables and then commit it".
It might be nice to do this automatically now that we have a working
.transactionfunction that takes a closure. Simply re-invoke the closure on40001.rich-murphey commentedon Jan 25, 2021
I'm interested in helping with this.
As a recent newcomer, I wished there were introductory on-boarding information, as a supplement to just one example to help folks get from zero familiarity to having one running example for one database.
rich-murphey commentedon Jan 25, 2021
Hey, I might help with sqlite examples. The idea of showing features and possibilities through examples is a very good point.
asnimansari commentedon Jan 28, 2021
Would like to contribute to more examples
human-bean-555 commentedon Jan 30, 2021
Hi - this library is great! 😄 I've been using it for a project and ran into some similar issues as described here when you start to get into table joins with 1:Many etc. and/or using the
ARRAY_AGGfor returningVec<T>; or structs withVec<T>. #298 seems to cover this.My current work in progress is to simply define a database specific type and then flatten the rows into my final type: since I've many types to do this with I made a trait FromDBFlatten (that gist has an example with joins). It then gives me a nice usage:
If anyone is looking for how to run multiple queries in one transaction (I couldn't find anything), I think this is the correct way:
mehcode commentedon Jan 30, 2021
Love the enthusiasm everyone. I will commit to getting this started over the next couple days (in the
nextbranch) and then would love additional help filling in.@pauldorehill Very nice! Coincidentally (and coming from a lot of the 1:M requests) we're planning on adding something like:
Which would allow something like:
A large improvement to this pattern would be to query and ignore some fields. This would remove the need for a
.mapor twoUsertypes.Perhaps something like:
human-bean-555 commentedon Jan 30, 2021
@mehcode that is an nice way of doing it too: I like domain transfer objects as way to over come the impedance miss match at boundaries. One other slightly cryptic error I've come across is
assertion failed: self.remaining() >= dst.len(): there is no compile time error but it comes at runtime. From what I've worked out its when your parent struct contains a child struct that doesn't have/match all the fields in the db table in something like:I'm not sure if I should open an issue, or if its a know limitation, but there isn't the same level of compile checking for child structs? You get a runtime error along the lines of
error occurred while decoding column 1: mismatched typesif you don't get the ordering of the SQL tuple to match the ordering of the struct fields e.g.SELECT giant.id AS id, giant.name AS name, (meal.name, meal.id) AS "meal!: Meal"compiles, but runtime errors sincenameis beforeidOr a
no field `0` found on recordif you were to writeSELECT giant.id AS id, giant.name AS name, meal.id AS "meal!: Meal"pythoneer commentedon Jan 30, 2021
I think this issue is getting filled with great ideas all based around the topic of "idiomatic usage of sqlx" and how to improve the current state. Do we need to branch out some new issues for these?
.transaction()that can retry on40001(should this be configurable? usable for other generic cases?)fetch_group_map_byquery_as - skipi really like this one. it would eliminate myAccountNoPoststruct if you really don't want to use.map. Do we have to wrap those skip{ed, able} fields inoption(that might interfere with other usages) or do we need those fields to deriveDefault?camsjams commentedon Feb 10, 2021
I made a presentation for Rust Lang Los Angeles which uses SQLx, actix and async-graphql, it showcases UUIDs and datetime fields which I found missing from the documentation:
https://github.com/camsjams/rust-actix-graphql-sqlx-postgresql
shanesveller commentedon Jul 23, 2021
Is there any new information, features or references for one-to-many or many-to-many relationships with
query_as!since the last activity here? I'm interested in a seemingly straightforward case where one struct has a member which is a Vec of another struct, for example. I've seen snippets aroundarray_aggand type hinting within the SQL, but I'm not able to successfully compile to deserialize the result so far.longfellowone commentedon Dec 16, 2021
Where you able to find any simple examples using array_agg?
FabienD commentedon May 20, 2022
Hello,
Yes, It'll be very interesting to have few examples of how to deal with relations between models. I use this library for my first "side-project" and It's difficult to find more complex example than a simple "todo list" :-)
In my example, I have 2 models, Tracking and TrackingLink. Tracking can have few TrackingLink (cf. links: Vec).
Bellow the SQL part, with the use of the macro sqlx::query_as! with the use of postgresql array_agg function.
This first example works.
After that, I wanted to add a filter to this SQL, and use a "non-macro" form of query_as :
It produces this error :
I don't understand why the first example work and the second one not.
Fabien.
mattoni commentedon Jul 1, 2022
An important item to note from my own experiments:
Using
array_agg()the listed fields in the parenthesis MUST be listed in the exact same order as the fields of your struct, otherwise it will either fail to decode at runtime, or the fields will have the incorrect values.THIS IS NOT CAUGHT AT COMPILE TIME
If your fields are out of order, you would get a runtime error something like this:
I wonder if there is a way to catch this in the macro to at least prevent accidental runtime bugs.
abonander commentedon Jul 1, 2022
That's unlikely to be something we can typecheck at compile time without performing our own analysis of the query.
mattoni commentedon Jul 1, 2022
Ah, thanks @abonander. It looked like there was another interesting proposal to do a
fetch_group_map_by, but I don't see anything in the PRs for that work. Is it currently being tackled by someone/on the roadmap still as far as you're aware?seguidor777 commentedon Nov 16, 2022
Can anyone please let me know if the nested queries are possible by using the
query_asfunction (not the macro). All I have found are examples using thequery_as!macrojb-alvarado commentedon Nov 16, 2022
The exact same question I was asking my self to. Yesterday I struggle some hours with the
query_asfunction, and now I found out, that with the macro it works.It would be very useful to have this feature to, in the query_as function. At the moment is not possible to build a dynamic nested query.
jb-alvarado commentedon Nov 28, 2022
Just for others when they end up here, and like me and @seguidor777 don't know how to manage complex structures with query_as function:
There is everything in the documentation, I just did not found it first. Lets say you have this tables:
For
MediaFileyou can create a custom FromRow trait:Then you can query the table with:
In this example speaker is optional, that is why I use
case when ms.id > 0 ....frederikhors commentedon Jan 11, 2023
Did you find a way? @shanesveller
Implement some JANKY AF data access layers
renato145 commentedon Aug 31, 2023
I was just needing to do a simple 1:many query and ended up using "array_agg" in the query and filtering empty values, then, I need to replace nulls with empty arrays:
frantisek-heca commentedon Mar 19, 2024
@jb-alvarado
I wonder where exactly have you found documentation you mentioned in your last post? I haven't found this exact type of problem solving anywhere yet. All I see are many examples of this type
(column1, column2) as "media_language!: Lang"in the documentation. Which implies various derives ofTypeorFromRowmagics (from a newcomer point of view) + confusion about usage ofOption<>vsTypederive does not allowOptionfields.. and so on.Just wondering about your statement that it is somehow clear in the docs.
jb-alvarado commentedon Mar 19, 2024
@frantisek-heca there is a section about manuell implementation it not give a more advanced example, but is a starting point. Here in the issues you found also some examples and explanations, also about the issue:
I don't know if this problem still exists, as I remember was not an issue by sqlx itself. I implemented my own type for the problem, here an example:
morzel85 commentedon Dec 27, 2024
Yes, having examples of recommend ways of handling one-to-many and many-to-many relationships would greatly help newcomers! This is a must-have for anything SQLish!
So far I've ended up doing this for one-to-many (DemoParent struct with a nested vector of DemoChildren, on PostgreSQL):
Disclaimer: I'm rust/sqlx/postgres/axum noob mysef, this is from a toy project :)
And this is how I get result from Axum endpoint (using shuttle.rs):
It gives me such JSON:
[ { "id": "P1", "name": "Parent 1", "children": [ { "id": "C1", "parent_id": "P1", "name": "Child 1" } ] }, { "id": "P2", "name": "Parent 2", "children": [ { "id": "C2", "parent_id": "P2", "name": "Child 2" }, { "id": "C3", "parent_id": "P2", "name": "Child 3" } ] }, { "id": "P3", "name": "Parent 3", "children": [ { "id": "C4", "parent_id": "P3", "name": "Child 4" }, { "id": "C5", "parent_id": "P3", "name": "Child 5" }, { "id": "C6", "parent_id": "P3", "name": "Child 6" } ] } ]aaronpeacock commentedon Jun 12, 2025
It's 2025 and I am still stumbling around trying to find any kind of basic usage guide. The postgres examples inside this repo don't have queries, for the most part, and certainly nothing serious. I don't see any guide just the API docs on crates.io
I literally found more info on how this crate works on this single issue here than in the entire documentation.
I am still unclear on what I must do to query_as a nested struct from a joined row.
If there are any guides to sqlx usage, perhaps the links need to be on this repo's homepage.
I didn't see any.
Most of the issues related to this seem to have dead-ended and I don't find any concrete solutions, or?
renato145 commentedon Jun 12, 2025
Can you give an example of what you want to do?
aaronpeacock commentedon Jun 13, 2025
or something like this.
I basically want to join 2 tables, each described by a struct, as a 3rd composite struct.
renato145 commentedon Jun 13, 2025
Probably you need to use https://docs.rs/sqlx/latest/sqlx/trait.FromRow.html, check the
#[sqlx(flatten)]attribute.