Help us understand the problem. What is going on with this article?

Ruby on RailsでDBのカラム追加をしてみた。

1.はじめに

1-1.現在のDBの確認

$ rails dbconsole
SQLite version 3.24.0 2018-06-04 14:10:15
Enter ".help" for usage hints.
sqlite> 
sqlite> .headers on
sqlite> select * from stores;

id|name|name_hurigana|smoking|created_at|updated_at|prefecture|city
1|山手十番館|(ヤマテジュウバンカン)|分煙|2019-08-29 12:57:40.109863|2019-08-29 12:57:40.109863

まずは現状のカラムの確認
id|name|name_hurigana|smoking
上記の4つが最初に作成した、カラムになります。

sqlite> .headers onを行わないとカラムが表示されないので注意

2.手順

2-1.DBのカラム追加

$ rails generate migration AddColumnToStore prefecture:string city:string

Column To Storeの最初の文字は大文字
Store のところに追加したいモデルを記入
prefecture:string city:string DB作成する時同様の名前とフォーマットの設定

2-2.ファイルが作成されるので確認

20190829091622_add_detail_to_store.rb
class AddDetailToStore < ActiveRecord::Migration[5.2]
  def change
    add_column :stores, :prefecture, :string
    add_column :stores, :city, :string
  end
end

実行を完了後は上記のようにファイルが作成されます。

2-3.DBの更新

rake db:migrate

忘れてはいけないmigrate
これでDBの追加完了。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away