我有一张桌子:
DB /迁移/ 20140731201801_create_voc_brands.rb:
DB /迁移/ 20140731201801_create_voc_brands.rb:
class CreateVocBrands < ActiveRecord::Migration def change create_table :voc_brands do |t| t.string :name t.timestamps end end end
但是我需要更改表(如果我从零创建它):
class CreateVocBrands < ActiveRecord::Migration def change create_table :voc_brands,:id => false do |t| t.uuid :id,:primary_key => true t.string :name t.timestamps end add_index :voc_brands,:id end end
如何使用迁移进行更改?
解决方法
我有与你同样的问题.要从默认ID迁移到使用uuid,我认为你可以类似于我所拥有的东西:
class ChangeVocBrandsPrimaryKey < ActiveRecord::Migration def change add_column :voc_brands,:uuid,default: "uuid_generate_v4()",null: false change_table :voc_brands do |t| t.remove :id t.rename :uuid,:id end execute "ALTER TABLE voc_brands ADD PRIMARY KEY (id);" end end