LoginSignup
0
0

More than 3 years have passed since last update.

テーブルのデータ型とコマンドについての備忘録(Rails)

Last updated at Posted at 2020-09-22

はじめに

テーブルを作る際、この場合データ型なんだったっけ?コマンドなんだったっけ?と調べ直してしまうことがままあるのでまとめることにしました。

データ型

データ型 種類
integer 数値(整数)
decimal 数値(精度の高い小数)
float 数値(浮動小数)
string 文字(短い文字列)
text 文字(長い文字列
date 日付
datetime 日時
time 時刻
timestamp タイムスタンプ
binary バイナリ
boolean 真偽

テーブル、カラム作成の際に使うコマンド

テーブル、カラムを作る時

$ rails g model モデル名 カラム名:データ型

使用例:Userモデルの作成と名前(name)と自己紹介文(introduction)のためのカラムを作成したいとき

$ rails g model User name:string introduction:text

テーブルを削除

$ rails d model モデル名

使用例

$ rails d model User

既存のテーブルに対しカラムの追加/削除するコマンド

カラムの追加

$ rails g migration Addカラム名Toテーブル名 カラム名:型名

使用例:Userテーブルにtitleカラムを追加したいとき

$ rails g migration AddTitleToUsers title:string

カラムの削除

$ rails g migration Removeカラム名Fromテーブル名 カラム名:型名

使用例:Userテーブルにtitleカラムを削除したいとき

$ rails g migration RemoveTitleFromUsers title:string

マイグレーション実行

作成、変更、削除が出来たらDBには反映させるため、db:migrateコマンドを実行する。

$ rails db:migrate
0
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
0
0