Skip to content

Field macro #[sqlx(flatten)] doesn't seem to work in this case #2584

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

Open
UMR1352 opened this issue Jul 5, 2023 · 1 comment
Open

Field macro #[sqlx(flatten)] doesn't seem to work in this case #2584

UMR1352 opened this issue Jul 5, 2023 · 1 comment
Labels

Comments

@UMR1352
Copy link

UMR1352 commented Jul 5, 2023

Bug Description

I have a struct defined as

#[derive(Debug, FromRow, Serialize)]
pub struct Building {
    pub name: String,
    pub key: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

and I want to make another where some other field is included (in this case conf):

#[derive(Debug, FromRow, Serialize)]
pub struct BuildingWithConfig {
    #[sqlx(flatten)]
    pub building: Building,
    pub conf: JsonValue,
}

In my mind this would work, but it doesn't:

error[E0560]: struct `structure::BuildingWithConfig` has no field named `key`
   --> src/routes/map.rs:96:5
    |
96  | /     sqlx::query_as!(
97  | |         BuildingWithConfig,
98  | |         r#"
99  | | SELECT
...   |
110 | |         is_preview
111 | |     )
    | |_____^ `structure::BuildingWithConfig` does not have this field
    |
    = note: available fields are: `conf`, `building`

Using this query:

SELECT
    b.name,
    b.description,
    b.key,
    c.configuration AS conf
FROM buildings b
    JOIN component_configurations c ON 
        c.id = 'page-building-' || b.key

Info

  • SQLx version: 0.6.3
  • SQLx features enabled: [
    "runtime-tokio-rustls",
    "macros",
    "postgres",
    "migrate",
    "offline",
    "time",
    "chrono",
    "json",
    ]
  • Database server and version: Postgres 14.6
  • Operating system: 6.4.1-arch1-1
  • rustc --version: 1.70
@UMR1352 UMR1352 added the bug label Jul 5, 2023
@saiintbrisson
Copy link
Contributor

Hi! This is a duplicate of #2188.
TL;DR: The query_as! macro does not use the FromRow implementation, though it might change in the future (#514).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants