5
4

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.

Ruby on rails で間違って :id を設定してprimary_keyと重複した場合の対策

Posted at

新規にアプリを作成してモデルのテーブルに間違って:idをマイグレーションすると
下記のようなエラーをはかれます。

rake aborted!
StandardError: An error has occurred, all later migrations canceled:

you can't redefine the primary key column 'id'. To define a custom primary key, pass { id: false } to create_table.

作成したidがprimary_keyのidと重複して邪魔しているということらしい。

id: falseをcreate_tableに書け、ということだけどどこに書いたらいいの?で探してブログRails4 db:migrateでid以外のカラムにプライマリキーの設定を行う
を参考にして下記のように書いて解決。primary_keyを追加で書いたidに改めて追加してしてます。

create_table :モデル名, :id => false do |t|
      t.integer :id, :primary_key => true
5
4
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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?