唯一性验证返回错误的非唯一性

时间:2019-01-23 16:57:37

标签: sql ruby activerecord

将状态添加到空数据库,其中状态validates :name, uniqueness: true

同样,数据库为空!

State.create(name:"Alabama")                                                                                                  
   (0.2ms)  BEGIN
  State Exists (0.6ms)  SELECT  1 AS one FROM "states" WHERE "states"."name" = $1 LIMIT $2  [["name", "Alabama"], ["LIMIT", 1]]
   (0.3ms)  ROLLBACK
=> #<State:0x00007ffb711c3c60 id: nil, name: "Alabama", region_id: nil, created_at: nil, updated_at: nil>
State.all                                                                                                                     
  State Load (0.5ms)  SELECT "states".* FROM "states"
=> []

为什么说当没有状态存在时这个状态存在呢?

当我摆脱唯一性验证时,它仍然表现得很有趣:

State.all                                                                                                                      
  State Load (0.7ms)  SELECT "states".* FROM "states"
=> []

0 个答案:

没有答案