Rails uniqueness validation not work 现在能解决了吗?

cysh · April 20, 2021 · Last by zhengpd replied at April 28, 2021 · 530 hits

😂

any sample?

Reply to ken

快速插入数据的时候唯一验证不工作

This helper validates that the attribute's value is unique right before the object gets saved. It does not create a uniqueness constraint in the database, so it may happen that two different database connections create two records with the same value for a column that you intend to be unique. To avoid that, you must create a unique index on that column in your database.

https://guides.rubyonrails.org/active_record_validations.html#uniqueness

加个数据库唯一索引?

db unique index.

Reply to Rei

数据库加索引,重复插入会报 500 error,对吗?如果是这样太不友好了叭,我害得写异常处理……

Reply to cysh

报不报错都应该在数据库加约束啊

Reply to cysh

500 比清除重复数据好处理,比如 rescue 之后返给前端个提示“xxx 被占用”

You need to Sign in before reply, if you don't have an account, please Sign up first.