0

I am working on a REST API written in Rust using axum, sqlx and PostgreSQL.

There are the following two models:

#[derive(FromRow, Serialize)]
pub struct Brand {
    id: i32,
    title: String,
    #[sqlx(flatten)]
    product: Product
}

#[derive(FromRow, Serialize)]
pub struct Product {
    title: String,
    id: i32,
}

Query code:

let rows = query_as::<_, Brand>("SELECT * FROM brands INNER JOIN products ON brands.id = products.brandId")
        .fetch_all(&pool)
        .await
        .unwrap();

And I get the following response:

{
    "id": 1,   // there should be 4
    "title": "AeroCool VX PLUS 500W",  // there should be just a "AeroCool"
    "product": {
            "title": "AeroCool VX PLUS 500W",
            "id": 1 
    }
},
{
    "id": 2,   // there should be 12
    "title": "Cougar VTE600",  // there should be just a "Cougar"
    "product": {
        "title": "Cougar VTE600",
        "id": 2
    }
},

I think this is due to the same field names.

I'm sorry if I didn't make myself clear, this is my first time asking a question here.

So far, I see only one way out - this is renaming the fields in the database. But I think it's a bad decision. Tell me, please, what can be done in this situation?

Yuri Astrakhan
  • 8,808
  • 6
  • 63
  • 97

0 Answers0