Many to Many as vector #2886
-
|
Hi I have a query with a few joins and would like my many to many relationship to be returned as a #[derive(Serialize, Deserialize, Queryable, Debug, Clone)]
pub struct Build {
pub id: DbId,
pub package: String,
pub upstream_version: String,
pub revision: Db32,
pub architectures: String,
pub firmware: String,
pub publisher: String,
pub insert_date: NaiveDateTime,
pub active: Option<bool>,
}
impl DbBuild {
pub fn find_all(conn: &Connection) -> Vec<Build> {
build::table
.limit(20)
.offset(0)
.inner_join(version::table.inner_join(package::table))
.inner_join(firmware::table)
.inner_join(user::table)
.inner_join(build_architecture::table.inner_join(architecture::table))
.select((
build::id,
package::name,
version::upstream_version,
version::ver,
architecture::code, // need as an array or vector
firmware::version,
user::username,
build::insert_date,
build::active))
.order(build::id.asc())
.load::<Build>(conn)
.expect("Error loading builds from db")
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
There is unfortunately now database independent way to express this behavior as part of your sql query. Postgres provides the |
Beta Was this translation helpful? Give feedback.
There is unfortunately now database independent way to express this behavior as part of your sql query. Postgres provides the
array_aggaggregate function that could be used in combination with group by to express such a behavior. I'm not aware similar extensions for other database systems.Another solution for this problem is to use separate queries here based in the API provided by
diesel::associations.Entity::belonging_to(...)only returns a query that can be used with otherQueryDslmethods like joins or select clauses.