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

Add support for multiple rename attributes #2186

Open
equt opened this issue Nov 2, 2022 · 1 comment
Open

Add support for multiple rename attributes #2186

equt opened this issue Nov 2, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@equt
Copy link

equt commented Nov 2, 2022

Is your feature request related to a problem? Please describe.

With the introduction of the flatten attribute, it's now possible to have many atomic structs composed together rather than defining the whole table into one struct. While there're cases where those atomic structs have different column names in different tables.

For example, giving the following User

struct User {
  pub id: i64,
  pub name: String,
}

The id might be named as user_id in user table, and owner_id in resource table.

Describe the solution you'd like

So instead of only allowing one rename, we might have an attribute that is possible to be called multiple times.

struct User {
  #[sqlx(rename = "user_id")]
  #[sqlx(rename = "owner_id")]
  pub id: i64,

  #[sqlx(rename = "user_name")]
  #[sqlx(rename = "owner_name")]
  pub name: String,
}

Describe alternatives you've considered

The current solution will be either define the struct manually multiple times or utilize a macro to do so.

Additional context

@equt equt added the enhancement New feature or request label Nov 2, 2022
@mdtusz
Copy link
Contributor

mdtusz commented Nov 16, 2022

I've encountered this issue as well, but am curious whether it could be instead done in a less macro-heavy way, instead relying on the sql query result. E.g.

#[derive(FromRow)]
struct User {
  pub id: i64,
  pub name: String,
}

#[derive(FromRow)]
struct Post {
  pub id: i64,
  pub name: String,
  pub author: User,
}

Then a query could require

SELECT p.id, p.name, (u.id, u.name) AS user 
FROM users AS u JOIN posts AS p 
ON p.author_id = u.id;

This would create a record for the user in the sql query result which I would think should be able to be deserialized into the struct.

Or maybe this is already possible and I haven't stumbled across the documentation or an example for it yet.

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
Development

No branches or pull requests

2 participants