84
59

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.

[エラー解決] Error: Duplicate column name ''"、カラムの重複、SQL

Last updated at Posted at 2019-01-18

エラー内容

カラムが重複しとるで! それはアカンで!っていうエラー

  1. $ rails db:migrate
  2. (最初の)エラーが発生する
  3. しかし、エラーのはずが、カラムがmigrationされてる。
  4. もう一度、$ rails db:migrate すると、下記のようなエラーが出る。
  5. 「なんでやねん!(最初の)エラーで、はじき出されたはずやろ?!」 謎やん!
Terminal.
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: Duplicate column name ''
Caused by: ActiveRecord::StatementInvalid: Mysql2::Error: Duplicate column name '': ALTER TABLE `` ADD ``
Caused by: Mysql2::Error: Duplicate column name 'likes_count'

解決策

####こういうときは migration ファイルを空にすればえーで!

<現状>

20190118141743_add_nickname_to_users.rb
class AddNicknameToUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :nickname, :integer
  end
end

↓ 
<これに変える>

20190118141743_add_nickname_to_users.rb
class AddNicknameToUsers < ActiveRecord::Migration[5.0]
  def change
# ココを空欄にする!!
  end
end
  1. Save
  2. $ rails db:migrate
    すると、
    == 20190118141743 AddNicknameToUsers: migrating ============================
    == 20190118141743 AddNicknameToUsers: migrated (0.0000s) ===================

migration成功!!

そのあと、下記の通りに戻しておく!

20190118141743_add_nickname_to_users.rb
class AddNicknameToUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :nickname, :integer
  end
end

以降、$ rails db:migrate してもエラーでぇ〜へんやろ??

参考URL

84
59
1

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
84
59

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?