1

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 :)

newBike
  • 14,385
  • 29
  • 109
  • 192

2 Answers2

0

This answer depends on what DBMS you're using.

You can of course drop the entire database and create it once again... but if you have data in other tables you care about then this is a bad idea.

I'm going to go right ahead and assume you're using one of SQLite3, MySQL or PostgreSQL.

Community
  • 1
  • 1
Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
0

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.

nyzm
  • 2,787
  • 3
  • 24
  • 30