Reputation: 811
I created a migration in rails using this command:
rails generate model Group name:string
and I ran rake db:migrate
. Now I want to add some properties to the 'name' like uniqueness and a default value. What are my options to achieve that? do I have to create a new migration for that?
Also let's say I will generate a new model called 'Department' which also has a unique and a default value, is there a way to add these options to the command directly?
Upvotes: 0
Views: 44
Reputation: 4940
Good question. It sort of depends on a few things, like if you are collaborating with others on this project or it's just you.
If it's just you, and you don't mind losing all the Groups
you have created, then you can rake db:rollback
and modify your migration file. Note, rake db:rollback
will only roll back 1 migration. To rollback x number of migrations, then rake db:rollback STEP=x
will work for you.
If you are collaborating with others (or if this app is in production), I would recommend just creating another migration file.
rails g migration update_groups_name
def up
change_column :groups, :name, :string, default: 'foo'
add_index :groups, :name, unique: true
end
def down
change_column_default :groups, :name, nil
remove_index :groups, :name
end
Upvotes: 2