title: ActiveRecord Uniqueness Constraint Returning Record Exists When It Doesn't
body: <p>I'm getting this output:</p>
  
  <pre><code>Patient Exists (0.3ms)  SELECT 1 AS one FROM "patients" WHERE "patients"."chrono_id" = 5668276 LIMIT 1
  </code></pre>
  
  <p>I know that a patient record with a chrono_id of that number does not exist, but I went to the console to check anyway.</p>
  
  <pre><code>Patient.where(chrono_id: 5668276)
  Patient Load (0.5ms)  SELECT "patients".* FROM "patients" WHERE "patients"."chrono_id" = 5668276
   =&gt; #&lt;ActiveRecord::Relation []&gt;
  </code></pre>
  
  <p>Console agrees with me.</p>
  
  <p>My model validation looks like</p>
  
  <pre><code>validates_uniqueness_of :chrono_id, :allow_blank =&gt; true, :allow_nil =&gt; true
  </code></pre>
  
  <p>If I try to create the patient in the console, I get the same error </p>
  
  <pre><code>Patient.create(chrono_id: 5668276, first_name: "a", last_name: "a", dob: "1988/12/11")
     (0.2ms)  BEGIN
    Patient Exists (0.4ms)  SELECT 1 AS one FROM "patients" WHERE "patients"."chrono_id" = 5668276 LIMIT 1
  </code></pre>
  
  <p>Which is reassuring because at least it means it isn't an environment issue.</p>
  
  <p>I tried to recreate the exact sql request that was coming back with a result so I could see the result and figure out when/how it was created</p>
  
  <pre><code>a = ActiveRecord::Base.connection.select_all("SELECT 1 AS one FROM patients WHERE patients.chrono_id = 5668276 LIMIT 1")
     (0.4ms)  SELECT 1 AS one FROM patients WHERE patients.chrono_id = 5668276 LIMIT 1
  
   =&gt; #&lt;ActiveRecord::Result:0x007fe0d343f650 @columns=["one"], @rows=[], @hash_rows=nil, @column_types={"one"=&gt;#&lt;ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::Integer:0x007fe0cef8bcc0&gt;}&gt;
  </code></pre>
  
  <p>There are no row values returned so I remain confused. Any ideas?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I had a typo that was entering one of the other required fields as nil. For some reason the Activerecord error didn't tell me there was an issue with this field but threw this unique_record issue instead. </p>
  
  <p><strong>Update 2</strong>
  The complete message in the logs was</p>
  
  <pre><code>   (0.1ms)  BEGIN
    Patient Exists (0.9ms)  SELECT 1 AS one FROM "patients" WHERE "patients"."chrono_id" = 5669845 LIMIT 1
     (0.2ms)  ROLLBACK
  </code></pre>