Rails migration for change column

Use #change_column. change_column(:table_name, :column_name, :date) # a few more examples: change_column(:suppliers, :name, :string, limit: 80) change_column(:accounts, :description, :text) NOTE: the same result can be achieved even outside of db migrations, this might be handy for testing/debugging but this method needs to be used very cautiously: ActiveRecord::Base.connection.change_column(:table_name, :column_name, :date)

How do you write a migration to rename an ActiveRecord model and its table in Rails?

Here’s an example: class RenameOldTableToNewTable < ActiveRecord::Migration def self.up rename_table :old_table_name, :new_table_name end def self.down rename_table :new_table_name, :old_table_name end end I had to go and rename the model declaration file manually. Edit: In Rails 3.1 & 4, ActiveRecord::Migration::CommandRecorder knows how to reverse rename_table migrations, so you can do this: class RenameOldTableToNewTable < ActiveRecord::Migration def change … Read more

How do I handle too long index names in a Ruby on Rails ActiveRecord migration?

Provide the :name option to add_index, e.g.: add_index :studies, [“user_id”, “university_id”, “subject_name_id”, “subject_type_id”], unique: true, name: ‘my_index’ If using the :index option on references in a create_table block, it takes the same options hash as add_index as its value: t.references :long_name, index: { name: :my_index }

Rails DB Migration – How To Drop a Table?

You won’t always be able to simply generate the migration to already have the code you want. You can create an empty migration and then populate it with the code you need. You can find information about how to accomplish different tasks in a migration here: http://api.rubyonrails.org/classes/ActiveRecord/Migration.html More specifically, you can see how to drop … Read more

How can I rename a database column in a Ruby on Rails migration?

rename_column :table, :old_column, :new_column You’ll probably want to create a separate migration to do this. (Rename FixColumnName as you will.): script/generate migration FixColumnName # creates db/migrate/xxxxxxxxxx_fix_column_name.rb Then edit the migration to do your will: # db/migrate/xxxxxxxxxx_fix_column_name.rb class FixColumnName < ActiveRecord::Migration def self.up rename_column :table_name, :old_column, :new_column end def self.down # rename back if you need … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)