{"record":{"id":"e3baa09d07bcc4d0","repo":"mattermost-community/focalboard","slug":"getblockhistorynewestchildren-unable-to-generate-s","errorCode":null,"errorMessage":"getBlockHistoryNewestChildren unable to generate subquery: %w","messagePattern":"getBlockHistoryNewestChildren unable to generate subquery: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/services/store/sqlstore/blocks.go","lineNumber":693,"sourceCode":"\tbuilder := s.getQueryBuilder(db).PlaceholderFormat(sq.Question)\n\n\tsub := builder.\n\t\tSelect(\"bh2.id\", \"MAX(bh2.insert_at) AS max_insert_at\").\n\t\tFrom(s.tablePrefix + \"blocks_history AS bh2\").\n\t\tWhere(sq.Eq{\"bh2.parent_id\": parentID}).\n\t\tGroupBy(\"bh2.id\")\n\n\tif opts.AfterUpdateAt != 0 {\n\t\tsub = sub.Where(sq.Gt{\"bh2.update_at\": opts.AfterUpdateAt})\n\t}\n\n\tif opts.BeforeUpdateAt != 0 {\n\t\tsub = sub.Where(sq.Lt{\"bh2.update_at\": opts.BeforeUpdateAt})\n\t}\n\n\tsubQuery, subArgs, err := sub.ToSql()\n\tif err != nil {\n\t\treturn nil, false, fmt.Errorf(\"getBlockHistoryNewestChildren unable to generate subquery: %w\", err)\n\t}\n\n\tquery := s.getQueryBuilder(db).\n\t\tSelect(s.blockFields(\"bh\")...).\n\t\tFrom(s.tablePrefix+\"blocks_history AS bh\").\n\t\tInnerJoin(\"(\"+subQuery+\") AS sub ON bh.id=sub.id AND bh.insert_at=sub.max_insert_at\", subArgs...)\n\n\tif opts.Page != 0 {\n\t\tquery = query.Offset(uint64(opts.Page * opts.PerPage))\n\t}\n\n\tif opts.PerPage > 0 {\n\t\t// limit+1 to detect if more records available\n\t\tquery = query.Limit(uint64(opts.PerPage + 1))\n\t}\n\n\tsql, args, err := query.ToSql()\n\tif err != nil {","sourceCodeStart":675,"sourceCodeEnd":711,"githubUrl":"https://github.com/mattermost-community/focalboard/blob/a84bbb65e32edf972856b329417096ac413518e9/server/services/store/sqlstore/blocks.go#L675-L711","documentation":"getBlockHistoryNewestChildren builds a subquery (max insert_at per block id) with squirrel; if sub.ToSql() fails to compile the SQL, this wrapped error is returned. It indicates the query builder could not render the constructed subquery, typically due to malformed clauses or empty/mismatched arguments.","triggerScenarios":"GetBlockHistoryNewestChildren invoked with option combinations that produce an unrenderable subquery — e.g. malformed opts fields (BeforeUpdateAt/AfterUpdateAt/PerPage handling) causing squirrel ToSql() to error.","commonSituations":"Programmatic callers passing inconsistent pagination/filter options; regressions after modifying the query-building code; unusual DB dialect setups interfering with clause construction.","solutions":["Check the wrapped err (%w) for the squirrel ToSql failure detail","Verify the opts passed to GetBlockHistoryNewestChildren are sane (non-negative PerPage, valid timestamps)","If you modified the subquery construction, confirm every Where/Select clause is well-formed and args align with placeholders","Report/patch upstream if a specific opts combination reliably breaks ToSql"],"exampleFix":"// before\nopts := store.BlockHistoryOpts{PerPage: -1}\nhist, hasMore, err := store.GetBlockHistoryNewestChildren(blockID, opts)\n// after\nif opts.PerPage < 0 {\n    opts.PerPage = store.BlockHistoryDefaultPerPage\n}\nhist, hasMore, err := store.GetBlockHistoryNewestChildren(blockID, opts)","handlingStrategy":"validation","validationCode":"func safeHistoryOpts(opts store.BlockHistoryOpts) store.BlockHistoryOpts {\n    if opts.PerPage <= 0 {\n        opts.PerPage = 25\n    }\n    return opts\n}","typeGuard":null,"tryCatchPattern":"hist, hasMore, err := store.GetBlockHistoryNewestChildren(blockID, opts)\nif err != nil {\n    if strings.Contains(err.Error(), \"unable to generate subquery\") {\n        return nil, fmt.Errorf(\"bad history opts %v: %w\", opts, err)\n    }\n    return nil, err\n}","preventionTips":["Never pass negative or zero PerPage in history options","Keep timestamps in opts as millisecond epoch values (BeforeUpdateAt/AfterUpdateAt)","Avoid custom patches to the subquery construction that can desync clauses and args","Pin squirrel dependency versions to those used by the server"],"tags":["sql","query-builder","history"],"backgroundTag":"sql-generation-failed","analyzedSha":"a84bbb65e32edf972856b329417096ac413518e9","analyzedAt":"2026-08-30T09:22:20.720Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}