--How to add / remove constraints to columns with NOT NULL constraints --How to delete index from column linked with multiple columns --How to delete a column with a foreign key created with reference type
def up
change_column_null :posts, :content, false
end
def down
change_column_null :posts, :content, true
end
The point is to use the up / down method instead of the change method.
def up
change_column_null :posts, :content, true
end
def down
change_column_null :posts, :content, false
end
def change
remove_index :likes, column: [:user_id, :post_id], unique: true
end
When deleting the index from a single column, do as follows
def change
remove_index :likes, column: :micropost_id, unique: true
end
def change
remove_reference :posts, :user, null: false, foreign_key: true
end
foreign_key: If you have set a foreign key with true, you should add it as well.
Check if a column has an index (in MySQL)
mysql > SHOW CREATE TABLE tbl_name
https://dev.mysql.com/doc/refman/5.6/ja/show-create-table.html
https://railsguides.jp/active_record_migrations.html
https://qiita.com/akinov/items/852fe789fe98a44350a9
Recommended Posts