{"record":{"id":"a58e6b9b07b4a176","repo":"discourse/discourse","slug":"query-returned-a-non-existent-post-id-result-po","errorCode":null,"errorMessage":"Query returned a non-existent post ID:\n#{result.post_id}","messagePattern":"Query returned a non-existent post ID:\n#(.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"app/services/badge_granter.rb","lineNumber":381,"sourceCode":"\n    query_plan = nil\n    # HACK: active record sanitization too flexible, force it to go down the sanitization path that cares not for % stuff\n    # note mini_sql uses AR sanitizer at the moment (review if changed)\n    query_plan = DB.query_hash(\"EXPLAIN #{sql} /*:backfill*/\", params) if opts[:explain]\n\n    sample = DB.query(grants_sql, params)\n\n    sample.each do |result|\n      unless User.exists?(id: result.id)\n        raise \"Query returned a non-existent user ID:\\n#{result.id}\"\n      end\n      unless result.granted_at\n        raise \"Query did not return a badge grant time\\n(Try using 'current_timestamp granted_at')\"\n      end\n      if opts[:target_posts]\n        raise \"Query did not return a post ID\" unless result.post_id\n        if Post.exists?(result.post_id).blank?\n          raise \"Query returned a non-existent post ID:\\n#{result.post_id}\"\n        end\n      end\n    end\n\n    { grant_count: grant_count, sample: sample, query_plan: query_plan }\n  rescue => e\n    { errors: e.message }\n  end\n\n  MAX_ITEMS_FOR_DELTA = 200\n  def self.backfill(badge, opts = nil)\n    return unless SiteSetting.enable_badges\n    return unless badge.enabled\n    return if badge.query.blank?\n\n    post_ids = user_ids = nil\n    post_ids = opts[:post_ids] if opts\n    user_ids = opts[:user_ids] if opts","sourceCodeStart":363,"sourceCodeEnd":399,"githubUrl":"https://github.com/discourse/discourse/blob/1b2d7253e854205d2c95c80b2c6a1cdd42e45059/app/services/badge_granter.rb#L363-L399","documentation":"Error \"Query returned a non-existent post ID:\n#{result.post_id}\" thrown in discourse/discourse.","triggerScenarios":"Thrown at app/services/badge_granter.rb:381 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"1b2d7253e854205d2c95c80b2c6a1cdd42e45059","analyzedAt":"2026-08-26T03:11:08.302Z","schemaVersion":2},"datasetVersion":"2026-08-26T07:17:17.940Z"}