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

cysh · 2021年04月20日 · 最后由 zhengpd 回复于 2021年04月28日 · 526 次阅读

😂

any sample?

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.

Rei 回复

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

cysh 回复

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

cysh 回复

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

需要 登录 后方可回复, 如果你还没有账号请 注册新账号