23
19

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 5 years have passed since last update.

Railsのmigrationでbigintを使う

Posted at

Rails5ではデフォルトで作成されるidがbigintになっているため、参照元テーブルのカラムをintegerで作成してしまうとforeign keyが作成できない。そのため、テーブルのカラムをbigintで作成する必要がある。

新規作成

def change
  create_table :articles do |t|
    t.bigint :author_id
  end
end

追加

def change
  add_column :articles, :author_id, :bigint
end

ちなみに、、、

def change
  create_tables :articles do |t|
    t.references :author
  end
end

としてもauthor_idがbigintで作成される。

23
19
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
23
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?