如何在Rails 3中创建表并拥有大量数据后重置主键(How to reset primary key after creating table and having lots data in Rai

编程入门 行业动态 更新时间:2024-10-27 05:34:27
如何在Rails 3中创建表并拥有大量数据后重置主键(How to reset primary key after creating table and having lots data in Rails 3)

如何重置主键(??)我想将我的主键设置为translate_str,让它在表中是唯一的,但我已经创建了表并在数据库中有很多日期。 我的简要代码在https://gist.github.com/poc7667/6076745谢谢你:)

How to reset primary key (??) I wanna set my primary key to translate_str , let it be unique in the table, But I've already create the table and have lots of date in the database. My Brief code is at https://gist.github.com/poc7667/6076745 Thanks you :)

最满意答案

如果您在translate_str列中有多个具有相同值的条目,那么更改表以使translate_str 唯一将给您一个错误。

如果收到错误,请删除重复记录,然后再次更改。

为了唯一性,您还可以使用Rails模型验证:

validates_uniqueness_of :translate_str

PS。 数据库中的UNIQUE属性优于validates_uniqueness_of。 如果两个请求同时由rails处理,则有时validates_uniqueness_of可能会失败。

If you have multiple entries with the same value in translate_str column, then altering your table to make translate_str unique will give you an error.

If you receive an error, remove duplicate records then do the altering again.

For uniqueness you can also use Rails model validation:

validates_uniqueness_of :translate_str

PS. UNIQUE attribute in database is better than validates_uniqueness_of. Cause validates_uniqueness_of might fail sometimes if two requests handled by rails at the same time.

更多推荐

本文发布于:2023-07-04 11:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023634.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主键   数据   如何在   reset   primary

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!