Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flatten for optionals #2934

Open
cemoktra opened this issue Dec 13, 2023 · 1 comment · May be fixed by #3667
Open

flatten for optionals #2934

cemoktra opened this issue Dec 13, 2023 · 1 comment · May be fixed by #3667
Labels
enhancement New feature or request

Comments

@cemoktra
Copy link
Contributor

cemoktra commented Dec 13, 2023

Is your feature request related to a problem? Please describe.
When selecting multiple columns from a left outer join (optional) i might get a set of 5 optional fields which i want to have in one optional struct that is none if nothing to join.

Describe the solution you'd like

#[derive(FromRow)]
struct DbRow {
  id_of_main_table: Uuid,
  otherstuff: (),
  #[sqlx(flatten)]
  inner: Option<Joined>
}

#[derive(FromRow)]
struct Joined {
  id_of_join: Uuid,
  more_joined_stuff: ()
}

Describe alternatives you've considered
Alternatives are having all fields optional and writing a huge tuple match to convert into an optional struct or use a helper crate like tuple_combinator

The manual approach would like:

#[derive(FromRow)]
struct DbRow {
  id_of_main_table: Uuid,
  otherstuff: (),
  //
  id_of_join: Option<Uuid>,
  more_joined_stuff: Option<()>,
}

// some conversion like
let joined = if let (Some(id_of_join),  Some(more_joined_stuff)) = (id_of_join, more_joined_stuff) {
  Some(Joined {
    id_of_join,
    more_joined_stuff
  })
} else {
  None
};
@cemoktra cemoktra added the enhancement New feature or request label Dec 13, 2023
@pravic
Copy link

pravic commented Apr 1, 2024

I've stumbled upon a series of Option in a query result type and wanted to refactor it into a single flattened Option<Inner> only to realize that this is not supported in sqlx, unlike in serde (playground).

But this should be easy, shouldn't it?

jonnyso added a commit to jonnyso/sqlx that referenced this issue Jan 5, 2025
jonnyso added a commit to jonnyso/sqlx that referenced this issue Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants