繁体   English   中英

如何使用锈柴油进行查询分组

[英]how to using rust diesel to do the group by query

我正在使用 锈柴油diesel = { version = "1.4.8", features = ["postgres","64-column-tables","chrono","serde_json"] }通过查询按照文档进行分组这个:

fpub fn get_bill_book_account_sum(){
use crate::diesel::GroupByDsl;
use diesel::dsl::max;
use crate::model::diesel::dict::dict_schema::test as bill_record_table;
let source_query = bill_record_table::table
    .group_by(bill_record_table::id)
    .select((max(bill_record_table::tags),bill_record_table::id))
    .filter(bill_record_table::dsl::tags.eq(9));
}

然后编译这段代码,显示如下错误:

error[E0277]: the trait bound `aggregate_ordering::max::max<BigInt, columns::tags>: NonAggregate` is not satisfied
   --> src/main.rs:19:17
    |
19  |         .select((max(bill_record_table::tags),bill_record_table::id))
    |          ------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `NonAggregate` is not implemented for `aggregate_ordering::max::max<BigInt, columns::tags>`
    |          |
    |          required by a bound introduced by this call
    |
    = note: required because of the requirements on the impl of `diesel::Expression` for `(aggregate_ordering::max::max<BigInt, columns::tags>, columns::id)`
note: required by a bound in `diesel::QueryDsl::select`
   --> /Users/xiaoqiangjiang/.cargo/registry/src/mirrors.tuna.tsinghua.edu.cn-df7c3c540f42cdbd/diesel-1.4.8/src/query_dsl/mod.rs:291:20
    |
291 |         Selection: Expression,
    |                    ^^^^^^^^^^ required by this bound in `diesel::QueryDsl::select`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `rust-learn` due to previous error    

我在哪里做错了? 我应该怎么做才能解决这个问题? 这是架构定义(我已经删除了所有列,仅使用 2 列来制作最小的重现示例):

table! {
    test (id) {
        id -> Int8,
        tags -> Int8,
    }
}

这是模型定义:

// Generated by diesel_ext

#![allow(unused)]
#![allow(clippy::all)]

use std::io::Write;
use diesel::deserialize::FromSql;
use diesel::pg::Pg;
use diesel::serialize::{Output, ToSql};
use diesel::sql_types::Jsonb;
use rocket::serde::Serialize;
use serde::Deserialize;
use crate::model::diesel::dict::dict_schema::*;

#[derive(Queryable,Debug,Serialize,Deserialize,Default)]
pub struct Test {
    pub id: i64,
    pub tags: i64,
}

这是最小的main.rs入口点:

#[macro_use]
extern crate diesel;

mod model;

use diesel::{ ExpressionMethods, QueryDsl};

fn main() {
    get_bill_book_account_sum();
}


pub fn get_bill_book_account_sum(){
    use crate::diesel::GroupByDsl;
    use diesel::dsl::max;
    use crate::model::diesel::dict::dict_schema::test as bill_record_table;
    let source_query = bill_record_table::table
        .group_by(bill_record_table::id)
        .select((max(bill_record_table::tags),bill_record_table::id))
        .filter(bill_record_table::dsl::tags.eq(9));
}

像这样更改聚合查询解决了这个问题:

pub fn get_bill_book_account_sum(request: &BillAccountRequest) -> Result<Vec<(i64, i32)>, diesel::result::Error>{
    use crate::diesel::GroupByDsl;
    use crate::model::diesel::fortune::fortune_schema::bill_record as bill_record_table;
    let source_query = bill_record_table::table
        .group_by(bill_record_table::account_id)
        .select((diesel::dsl::sql::<diesel::sql_types::BigInt>("SUM(CAST(amount AS Integer))"),bill_record_table::account_id))
        .filter(bill_record_table::dsl::bill_book_id.eq(request.bill_book_id));
    let result = source_query.load::<(i64,i32)>(&get_connection());
    return result;
}

解决方案来自这个问题 这是来自维护者的答案,表明柴油 1.x 官方不支持组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM