When I was creating a portfolio and wanted to add columns later, or when I wanted to change the type of the column that was set initially, I did not know what to do, so even if you are a beginner, even if you are a beginner I have summarized it for easy understanding. (At first, I thought I should add or change the existing migration file directly, but that is not correct, so I will create another migration file and describe how to add or change it. )
When you create a model with the following command, a migration file for creating the table that this model is in charge of is automatically created together with the model.
$ rails g model [Model name] [Column name]:[Column type]
For example, if you want to create a User model, specify User for [Model Name] and name: string email: string for [Column Name: Column Type]. The command looks like this:
$ rails g model User name:string email:string
When you execute the command, the following migration file will be created.
db/migrate/XXXXXXXXXXXXXX_create_users.rb
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :name
t.string :email
t.timestamps
end
end
end
Confirm that the above file is generated, and execute the migration with the following command.
$ rails db:migrate
The Users table is now created.
If you want to add columns to an already created table, create a new migration and add it instead of writing directly to the existing migration file.
To create a migration file to add a column, use the following command.
$ rails g migration Add[Column name]To[table name] [Column name]:[Column type]
For example, if you want to add an introduction column (text type) to the existing users table, it will be as follows.
$ rails g migration AddIntroductionToUsers introduction:text
When you execute the command, the following migration file will be created.
db/migrate/XXXXXXXXXXXXXX_add_introduction_to_users.rb
class AddIntroductionToUsers < ActiveRecord::Migration[6.0]
def change
add_column :users, :introduction, :text
end
end
Confirm that the above file is generated, and execute the migration with the following command.
$ rails db:migrate
The introduction column (text type) is now added to the users table.
If you want to change the column type of an existing table, you can change it by the same procedure as adding a column.
To create a migration file to change the column type, use the following command.
$ rails g migration change_data_[Column name]_to_[table name]
For example, if you want to change the type of the material column of the existing productions table, do as follows.
$ rails g migration change_data_material_to_productions
When you execute the command, a migration file will be created, so add the column type you want to change. For example, if you want to change the type of the material column of the productions table to the integer type, add as follows.
db/migrate/XXXXXXXXXXXXXX_change_data_material_to_productions.rb
class ChangeDataMaterialToProductions < ActiveRecord::Migration[6.0]
<!-- *****Add the following***** -->
def change
change_column :productions, :material, :integer
end
<!-- *****Add more***** -->
end
After the addition is completed, execute the migration with the following command.
$ rails db:migrate
The type of the material column in the productions table is now changed to integer type.
This article is the first to summarize in Qiita what beginners learned while creating a portfolio.
If there are any mistakes in the content, I would be grateful if you could comment.