title: Rails auto-assigning id that already exists
body: <p>I create a new record like so:</p>
  
  <p><code>truck = Truck.create(:name=&gt;name, :user_id=&gt;2)</code></p>
  
  <p>My database currently has several thousand entities for truck, but I assigned the id's to several of them, in a way that left some id's available.  So what's happening is rails creates item with id = 150 and it works fine.  But then it tries to create an item and assign it id = 151, but that id may already exist, so I'm seeing this error:</p>
  
  <p><code>ActiveRecord::RecordNotUnique (PG::Error: ERROR:  duplicate key value violates unique constraint "companies_pkey"
  DETAIL:  Key (id)=(151) already exists.</code></p>
  
  <p>And the next time I run the action, it will simply assign the id 152, which will work fine if that value isn't already taken.  How can I get rails to check whether an ID already exists before it assigns it?</p>
  
  <p>Thanks!</p>
  
  <p>EDIT</p>
  
  <p>The Truck id is what is being duplicated.  The user already exists and is a constant in this case.  It actually is a legacy issue that I have to deal with.  One option, is to re-create the table at let rails auto assign every id this time around.  I'm beginning to think this may be the best choice because I'm have a few other problems, but the migration for doing this would be very complicated because Truck is a foreign key in so many other tables.  Would there be a simple way to have rails create a new table with the same data already stored under Truck, with auto-assigned ID's and maintaining all existing relationships?</p>