我正在使用SqlAlchemy-Datatables来实现服务器端数据表。
我无法在服务器端为sqlalchemy数据表定义ColumnsDT和查询聚合函数,如sum,count等。我试过了
columns = [
ColumnDT(Timepass.user_name),
ColumnDT(func.sum(Timepass.income).label('Sum'))
]
results = db.session.query(Timepass.user_name, func.sum(Timepass.income).label('Sum')).group_by(Timepass.user_name)
params = request.args.to_dict()
rowTable = DataTables(params, results, columns)
return jsonify(rowTable.output_result())
我收到以下错误: DataTables警告:表id = example - (psycopg2.ProgrammingError)WHERE中不允许聚合函数 第4行:... ST(timepass.user_name AS TEXT)ILIKE'%%'或CAST(总和(timepa ...
答案 0 :(得分:1)
得到https://github.com/Pegase745/sqlalchemy-datatables/issues/93的帮助。这里要注意的关键是global_search = False参数。如果没有使用它,它会在where子句中抛出错误,说子集函数不能在Where子句中使用。
columns = [
ColumnDT(Timepass.user_name),
ColumnDT(func.sum(Timepass.income).label('Sum'), global_search=False),
ColumnDT(func.count(Timepass.user_name).label('Count'), global_search=False)
]
query = session.query(Timepass.user_name, func.sum(Timepass.income).label('Sum'),
func.count(Timepass.user_name).label('Count')).group_by(Timepass.user_name)
params = request.args.to_dict()
rowTable = DataTables(params, query, columns)
return jsonify(rowTable.output_result())