LoginSignup
0
0

More than 3 years have passed since last update.

Rails migrationで指定できるデータ型

Last updated at Posted at 2020-03-02

概要

railsではdb:migrationでmigrationファイルに記述されたデータ型をRDBに追加できる。

今回はmigrateに定義できるデータ型の種類についてまとめていこうと思います。

そもそもmigrationって何?

rails側からDBの設定(カラム、テーブルなど)を変更するためのファイル。

migrationを使うことでSQL文を使うことなく、DBの設定を変更することができる。

migrationコマンド

rails g migration sample

上のコマンドを打ち込むことでdb/migrateにsampleクラスのmigrationファイルが作成される。

[migration作成日時].rb
class CreateSamples < ActiveRecord::Migration[5.2]
  def change
    create_table :samples do |t|
      t.strings :name
      t.timestamps
    end
  end
end

こちらが作成されたmigrationファイル。

tの後に続いているstringsやtimestampsがデータ型になる。

migrationで指定できるデータ型は?

migrationはあくまでrails側で行うDBの操作

Mysql2などで使えるデータ型の中にはmigrationで指定できないものもある。

  • string
  • text
  • integer
  • binary
  • boolean
  • date
  • datetime
  • time
  • timestamp
  • decimal
  • float
  • primary_key

以上がmigrationで指定できるデータ型になります。

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