title: How is this validation error possible for this code?
body: <p>The error is <code>ActiveRecord::RecordInvalid: Validation failed: Route must exist</code>.</p>
  
  <p>This is the code:</p>
  
  <pre><code>new_route = Route.create!(new_route_data)
  
  new_points.each_with_index do |point, index|
    new_point_data = { route_id: new_route.id,
                       latitude: point[0],
                       longitude: point[1],
                       timestamp: point[2] }
    new_point = Point.create!(new_point_data)
  end
  </code></pre>
  
  <p>It is being reported for the <code>new_point = Point.create!(new_point_data)</code> line.</p>
  
  <p>Related Details:</p>
  
  <ul>
  <li>This code is running in a single Sidekiq worker as you see it above (so, the Route isn't being created in one worker, with the Points being created in another worker - this is all inline)</li>
  <li>The <code>routes</code> table has almost 3M records</li>
  <li>The <code>points</code> table has about 2.5B records</li>
  <li>The <code>Point</code> model contains <code>belongs_to :route, counter_cache: true</code></li>
  <li>There are no validations on the <code>Route</code> model</li>
  <li>In case it's relevant, the <code>Route</code> model does contain <code>belongs_to :user, counter_cache: true</code></li>
  <li>There are only about 5k records in the <code>users</code> table</li>
  </ul>
  
  <p>Software versions:</p>
  
  <ul>
  <li>Rails 5.1.5</li>
  <li>Ruby 2.5.0</li>
  <li>PostgreSQL 9.6.7</li>
  </ul>