How to add 'auto_increment' to another column besides 'id' in rails table defination?
If I need to add 'auto_increment' to a new column called 'another_id' in a table, how can I make it?
Is there an option like:create_table :posts do |t|
t.integer :another_id, :auto_increment => true # Is there a option like this?
...
t.timestamps
end
In development env I use sqlite3开发者_开发百科, and mysql in production env;
I don't believe the rails api for create_table has a autoincrement option. in the up method after the create_table you might have to execute a ALTER TABLE sql command to set the column to autoincrement. Note that this could be different for database vendors.
syntax is something like: execute "ALTER TABLE tbl_name MODIFY col_name AUTO INCREMENT"
or you could just run the execute command in the up method to create the table using a sql create table statement.
精彩评论