title: Rails - batch sql query - Primary key not included in the custom select clause
body: <p>I have a legacy database with multiple existing tables, I want to query thousands of records in one legacy table that <strong>doesn't have primary key</strong> </p>
  
  <pre><code>   LegacyTable.where(condition, start_date, end_date).find_each do |record|
        yield record
      end
  </code></pre>
  
  <p>but it will throw the bellow error:</p>
  
  <pre><code>ORA-01741: illegal zero-length identifier
  </code></pre>
  
  <p>because it auto generates <code>order by "LegacyTable".""</code>
  according to the <a href="http://api.rubyonrails.org/classes/ActiveRecord/Batches.html" rel="nofollow">api</a></p>
  
  <blockquote>
    <p>NOTE: It's not possible to set the order. That is automatically set to ascending on the primary key (“id ASC”) to make the batch ordering work. This also means that this method only works with integer-based primary keys.</p>
  </blockquote>
  
  <p>Then I set <code>self.primary_key = 'TRANSACTION_ID'</code> in the model, but TRANSACTION_ID is just an index in legacy table. Then it throws another exception:</p>
  
  <pre><code>RuntimeError (Primary key not included in the custom select clause):
  </code></pre>
  
  <p>How to do batch query in rails? If use Java, I can just add fetchSize parameter and don't need primary key in table.</p>