title: Rails on heroku: after push, get "PG::UniqueViolation: ERROR: duplicate key value violates unique constraint"
body: <p>This has been asked several times before (<a href="https://stackoverflow.com/questions/24957863/devise-primary-key-error-on-postgresql-heroku-rails-4">here</a> and <a href="https://stackoverflow.com/questions/11068800/rails-auto-assigning-id-that-already-exists">here</a>, and more).</p>
  
  <p>Every time I push my rails app to Heroku (for at least the last few months, I'd say), I have to reset my keys using the familiar</p>
  
  <pre><code>ActiveRecord::Base.connection.tables.each { |t|    ActiveRecord::Base.connection.reset_pk_sequence!(t) }
  </code></pre>
  
  <p>incantation.  Otherwise I get postgresql failures like this when I try to create new records:</p>
  
  <pre><code>PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "users_clients_pkey" DETAIL: Key (id)=(401) already exists. : INSERT INTO "users_clients" ("user_id", "client_id") VALUES (20, 46) RETURNING "id"
  </code></pre>
  
  <p>(This is an example; it happens on various tables, depending on what the first action is that's done on the app after a push.)</p>
  
  <p>Once I do the reset-keys incantation, it's fine until my next push to heroku... even when my push does not include any migrations.</p>
  
  <p>I'm a little baffled as to why this is happening and what can be done to prevent it.</p>
  
  <p>No, there's no datatable manipulation code in my deployment tasks.</p>