{"record":{"id":"be6dee04b21ebdc1","repo":"theonedev/onedev","slug":"cannot-order-by-field-be6dee","errorCode":null,"errorMessage":"Cannot order by field: ","messagePattern":"Cannot order by field: ","errorType":"exception","errorClass":"ExplicitException","httpStatus":null,"severity":"error","filePath":"server-core/src/main/java/io/onedev/server/search/entity/codecomment/CodeCommentQuery.java","lineNumber":256,"sourceCode":"\t\t\t\t\t\treturn new AndCriteria<>(childCriterias);\n\t\t\t\t\t}\n\n\t\t\t\t\t@Override\n\t\t\t\t\tpublic Criteria<CodeComment> visitNotCriteria(NotCriteriaContext ctx) {\n\t\t\t\t\t\treturn new NotCriteria<>(visit(ctx.criteria()));\n\t\t\t\t\t}\n\n\t\t\t\t}.visit(criteriaContext);\n\t\t\t} else {\n\t\t\t\tcommentCriteria = null;\n\t\t\t}\n\n\t\t\tList<EntitySort> commentSorts = new ArrayList<>();\n\t\t\tfor (OrderContext order : queryContext.order()) {\n\t\t\t\tvar fieldName = getValue(order.Quoted().getText());\n\t\t\t\tvar sortField = SORT_FIELDS.get(fieldName);\n\t\t\t\tif (sortField == null)\n\t\t\t\t\tthrow new ExplicitException(\"Cannot order by field: \" + fieldName);\n\n\t\t\t\tEntitySort commentSort = new EntitySort();\n\t\t\t\tcommentSort.setField(fieldName);\n\t\t\t\tif (order.direction != null) {\n\t\t\t\t\tif (order.direction.getText().equals(\"desc\"))\n\t\t\t\t\t\tcommentSort.setDirection(DESCENDING);\n\t\t\t\t\telse\n\t\t\t\t\t\tcommentSort.setDirection(ASCENDING);\n\t\t\t\t} else {\n\t\t\t\t\tcommentSort.setDirection(sortField.getDefaultDirection());\n\t\t\t\t}\n\t\t\t\tcommentSorts.add(commentSort);\n\t\t\t}\n\n\t\t\treturn new CodeCommentQuery(commentCriteria, commentSorts);\n\t\t} else {\n\t\t\treturn new CodeCommentQuery();\n\t\t}","sourceCodeStart":238,"sourceCodeEnd":274,"githubUrl":"https://github.com/theonedev/onedev/blob/d44925c47c37992c828ea673a5f9620539bc3ff2/server-core/src/main/java/io/onedev/server/search/entity/codecomment/CodeCommentQuery.java#L238-L274","documentation":"After parsing the criteria, parse() iterates the query's 'order by' clauses and looks each field name up in CodeComment.SORT_FIELDS. If the field is not sortable for code comments, ExplicitException \"Cannot order by field: <name>\" is thrown. Only fields registered in SORT_FIELDS can be used in 'order by'.","triggerScenarios":"A query with an order clause on a non-sortable field, e.g. \"order by content\" or any field name not present in SORT_FIELDS (typically 'create date' and 'last activity date' are sortable).","commonSituations":"Copy-pasting an order clause from another entity's query (issue/build queries have different sortable fields); typos in the field name; a field that became non-sortable after a OneDev upgrade.","solutions":["Use only sortable fields in 'order by', e.g. \"order by \\\"Create Date\\\"\" or \"order by \\\"Last Activity Date\\\"\".","Check CodeComment.SORT_FIELDS (or the UI's order-by dropdown) for the exact allowed field names.","Remove the order clause if default ordering is acceptable.","Catch ExplicitException around parse() to give the user a friendly message listing valid sort fields."],"exampleFix":"// before\nvar query = CodeCommentQuery.parse(project, \"Resolved order by \\\"Content\\\"\", true); // not sortable\n// after\nvar query = CodeCommentQuery.parse(project, \"Resolved order by \\\"Last Activity Date\\\" desc\", true);","handlingStrategy":"validation","validationCode":"// only sortable fields may appear in 'order by'\nstatic boolean isSortableField(String fieldName) {\n    return fieldName.equals(\"Create Date\") || fieldName.equals(\"Last Activity Date\");\n}\n// check every field after 'order by' in the query text before calling parse()","typeGuard":null,"tryCatchPattern":"try {\n    var query = CodeCommentQuery.parse(project, queryString, true);\n} catch (ExplicitException e) {\n    if (e.getMessage().startsWith(\"Cannot order by field\"))\n        showUserError(e.getMessage() + \" — sortable fields: Create Date, Last Activity Date\");\n    else\n        throw e;\n}","preventionTips":["Only use fields listed in CodeComment.SORT_FIELDS in order clauses.","Copy order clauses only from code comment queries, not other entity types.","Test saved queries with order clauses after upgrades."],"tags":["search-query","sorting","query-parser"],"backgroundTag":"invalid-query-parameter","analyzedSha":"d44925c47c37992c828ea673a5f9620539bc3ff2","analyzedAt":"2026-09-06T07:18:27.995Z","contentChangedAt":"2026-09-06T07:18:27.995Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}