1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

テーブルにカラムを追加する方法

Last updated at Posted at 2021-01-18

今回はmigrateファイルの記述をして追加する方法を実行します。

#migrateファイルを編集
まず、README.mdを参考にmigrateファイルを編集します。

README.md
## items テーブル

| 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 rails db:migrateの実行
記述が終わったら以下2点の実行

% rails db:rollback 
% rails db:migrate         

最後に
テーブルの確認
60cd92386af9d46922673f58e4fc6d2d.png

これでテーブルの追加が完了です!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?