diff --git a/lib/gitlab/background_migration/cleanup_optimistic_locking_nulls.rb b/lib/gitlab/background_migration/cleanup_optimistic_locking_nulls.rb
index 3aa1ebb49f94a43eb6b6724cb3e5b9650b91bd36..bf69ef352ccd187fc8e90d1be2560873f87228e4 100644
--- a/lib/gitlab/background_migration/cleanup_optimistic_locking_nulls.rb
+++ b/lib/gitlab/background_migration/cleanup_optimistic_locking_nulls.rb
@@ -17,7 +17,7 @@ module Gitlab
         (start_id..stop_id).each_slice(QUERY_ITEM_SIZE).each do |range|
           model
               .where(lock_version: nil)
-              .where(id: range)
+              .where("ID BETWEEN ? AND ?", range.first, range.last)
               .update_all(lock_version: 0)
         end
       end