・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina
$ rails g migration ChangeColumnToBooks
is
Same as writing $ rails generation migration change_column_to_books
.
In other words, generation
can be abbreviated as g
,
Like ʻAddBodyToBooks`, you can save the trouble of writing" _ "by capitalizing the beginning of a word.
$ rails g model model name column name: type name
Terminal
$ rails g model Book title:string
migrate/~_create_books.rb
class CreateBooks < ActiveRecord::Migration[5.2]
def change
create_table :books do |t|
t.string :title
t.timestamps
end
end
end
rails d model model name
Terminal
$ rails d model Book
Terminal
$ rails db:migrate
** ① When going back one step **
Terminal
$ rails db:rollback
** ① When returning to multiple steps **
Terminal
$ rails db:rollback STEP=5 #Numbers can be changed freely
Terminal
rails db:migrate:status
$ rails g migration Drop table name
Terminal
$ rails g migration DropBooks
migrate/~_drop_books.rb
class DropBooks < ActiveRecord::Migration[5.2]
def change
drop_table :books #Postscript
end
end
$ rails g migration Rename Table name before change To Table name after change
Terminal
$ rails g migration RenameBooksToArticles
migrate/~_rename_books_to_articles.rb
class RenameBooksToArticles < ActiveRecord::Migration[5.2]
def change
rename_table :books, :articles #Postscript
end
end
** ① Single **
$ rails g migration Add Column name To Table name Column name: Type name
Terminal
$ rails g migration AddBodyToBooks body:text
migrate/~_add_body_to_books.rb
class AddBodyToBooks < ActiveRecord::Migration[5.2]
def change
add_column :books, :body, :text
end
end
** ② Multiple **
$ rails g migration AddColumnsTo Table name Column name: Type name Column name: Type name Column name: Type name
Terminal
$ rails g migration AddColumnsToBooks body:text introduction:text price:integer
migrate/~_add_columns_to_books.rb
class AddColumnsToBooks < ActiveRecord::Migration[5.2]
def change
add_column :books, :body, :text
add_column :books, :introduction, :text
add_column :books, :price, :integer
end
end
** ① Single **
$ rails g migration Remove Column name From table name Column name: Type name
Terminal
$ rails g migration RemoveTitleFromBooks title:string
migrate/~_remove_title_from_books.rb
class RemoveTitleFromBooks < ActiveRecord::Migration[5.2]
def change
remove_column :books, :title, :string
end
end
** ② Multiple **
$ rails g migration RemoveColumnsFrom table name column name: type name column name: type name column name: type name
Terminal
$ rails g migration RemoveColumnsFromBooks body:text introduction:text price:integer
migrate/~_remove_columns_from_books.rb
class RemoveColumnsFromBooks < ActiveRecord::Migration[5.2]
def change
remove_column :books, :body, :text
remove_column :books, :introduction, :text
remove_column :books, :price, :integer
end
end
$ rails g migration ChangeData Column name To Table name Column name: Type name
Terminal
$ rails g migration ChangeDataTitleToBooks
migrate/~_change_data_title_to_books.rb
class ChangeDataTitleToBooks < ActiveRecord::Migration[5.2]
def change
change_column :books, :title, :text #Postscript
end
end
$ rails g migration ChangeOption Column name To Table name Column name: Type name
Terminal
$ rails g migration ChangeOptionTitleToBooks
migrate/~_change_option_title_to_books.rb
class ChangeOptionTitleToBook < ActiveRecord::Migration[5.2]
def change
change_column :books, :title, :string, null: false #Postscript
end
end
Model name | role |
---|---|
string | Short string |
text | Long string |
integer | integer |
float | Floating point |
decimal | Highly accurate decimal |
datetime | Date and time |
timestamp | Time stamp |
time | time |
date | date |
binary | Binary string |
boolean | Boolean value |
Option name | role |
---|---|
default | Set initial value |
null | The truth of the blank |
limit | Limit length |
unique | Give unique constraints |
unique | Give index |
Recommended Posts