[RUBY] How to add columns to a table

This time, we will describe the migrate file and add it.

Edit migrate file

First, edit the migrate file by referring to README.md.

README.md


##items table

| Column            | Type       | Options           |
| ----------------- | ---------- | ----------------- |
| name              | string     | null: false       |
| explanation       | text       | null: false       |
| category_id       | integer    | null: false       |
| state_id          | integer    | null: false       |
| delivery_price_id | integer    | null: false       |
| prefectures_id    | integer    | null: false       |
| delivery_time_id  | integer    | null: false       |
| price             | integer    | null: false       |
| user              | references | foreign_key: true |

### Association

- belongs_to :user
- has_one :purchase

20210118070956_create_items.rb


class CreateItems < ActiveRecord::Migration[6.0]
  def change
    create_table :items do |t|
      t.string :name,                   null: false
      t.text :explanation,              null: false
      t.integer :category_id,           null: false
      t.integer :state_id,              null: false
      t.integer :delivery_price_id,     null: false
      t.integer :prefectures_id,        null: false
      t.integer :delivery_time_id,      null: false
      t.integer :price,                 null: false
      t.references :user,               foreign_key: true
      t.timestamps                      null: false
    end
  end
end

Rails db: rollback Run rails db: migrate

After finishing the description, execute the following two points

% rails db:rollback 
% rails db:migrate         

Finally Check the table 60cd92386af9d46922673f58e4fc6d2d.png

This completes adding the table!

Recommended Posts

How to add columns to a table
[Xcode] How to add a README.md file
How to add a new hash / array
How to add / remove Ruby on Rails columns
How to add a classpath in Spring Boot
[Rails] How to create a table, add a column, and change the column type
How to add ActionText function
How to leave a comment
Create table and add columns
How to insert a video
How to add HDD to Ubuntu
How to create a method
How to clear all data in a particular table
How to store Rakuten API data in a table
How to add Hyperledger Iroha Peer
How to make a Java container
How to sign a Minecraft MOD
How to make a JDBC driver
[Rails] How to add new pages
[Java] How to create a folder
How to write a ternary operator
[Swift] How to send a notification
How to make a splash screen
How to make a Jenkins plugin
How to add the same Indexes in a nested array
How to make a Maven project
How to join a table without using DBFlute and sql
How to add the delete function
How to make a Java array
How to create a registration / update function where the table crosses
How to execute a contract using web3j
How to add local jar to maven pom.xml
How to sort a List using Comparator
How to make a Java calendar Summary
A memorandum on how to use Eclipse
How to redo a deployment on Heroku
Migration file to add comment to Rails table
[Basic] How to write a Dockerfile Self-learning ②
How to insert a video in Rails
[Java] How to add data to List (add, addAll)
How to add jar file in ScalaIDE
[Introduction to Java] How to write a Java program
How to create a Maven repository for 2020
How to make a Discord bot (Java)
How to print a Java Word document
[Swift5] How to create a splash screen
[rails] How to create a partial template
How to publish a library in jCenter
Ruby: CSV :: How to use Table Note
How to add application version information to Sentry information
[SpringBoot] How to write a controller test
[IntelliJ IDEA] How to automatically add final when saving a Java file
How to deploy
Rails: How to write a rake task nicely
How to create a database for H2 Database anywhere
[Rails] Add page nation to table [For beginners]
[Java] Add quotes only to specific CSV columns
[Rails] How to write when making a subquery
How to deploy a container on AWS Lambda
[Rails] How to create a graph using lazy_high_charts
[Android] How to convert a character string to resourceId