title: RoR/Postgres - Error with two column unique index. "duplicate key value violates unique constraint", even without any records
body: <p>So Contract can have many Services. and the join table is ContractsService(contracts_services), pretty simple.</p>
  
  <p>Index set with:</p>
  
  <pre><code>def change
    add_index :contracts_services, [:contract_id, :service_id], unique: true
  end
  </code></pre>
  
  <p>If I try to add like this (or via the API, or in any way, so not specific to this piece of code):</p>
  
  <pre><code>ContractsService.where(contract_id: 16, service_id: 17).first_or_create!
  </code></pre>
  
  <p>I get this error: </p>
  
  <pre><code>ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_contracts_services_on_contract_id_and_service_id"
  DETAIL:  Key (contract_id, service_id)=(16, 17) already exists.
  </code></pre>
  
  <p>However:</p>
  
  <pre><code>irb(main):015:0&gt; ContractsService.where(contract_id: 16, service_id: 17).count
  D, [2017-10-26T07:25:01.782432 #4] DEBUG -- :    (1.7ms)  SELECT COUNT(*) FROM "contracts_services" WHERE "contracts_services"."deleted_at" IS NULL AND "contracts_services"."contract_id" = $1 AND "contracts_services"."service_id" = $2  [["contract_id", 16], ["service_id", 17]]
  =&gt; 0
  </code></pre>
  
  <p>I'm stuck here, never seen this before. I'm I missing something simple? or is the index corrupt? Something else?</p>