4
1

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.

【備忘録】RailsTutorialの6.2.5でハマったのでメモ

Last updated at Posted at 2016-11-28

順番にやっていったのになぜかdb:migrateでコケる

チュートリアルの6.2.5を実施していたときの出来事。

###環境

  • mac OS X Yosemite(10.10.5)
  • ruby 2.3.1p112
  • rails 1.5.0.0.1

###どうコケたのか

rails generate migration add_index_to_users_email

これを実行して出来上がったファイルに

class AddIndexToUsersEmail < ActiveRecord::Migration
  def change
    add_index :users, :email, unique: true
  end
end

サンプルの通り上記を入力して、bundle exec rake db:migrateを実行したら

ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: users.email: CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email")

このエラー。
レコードがユニークじゃないよと。

###どう解決したのか
なんでやとおもいrails consoleで確認。

irb(main):004:0> User.all
  User Load (0.1ms)  SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation [#<User id: 298486374, name: "MyString", email: "MyString", created_at: "2016-11-28 13:29:24", updated_at: "2016-11-28 13:29:24">, #<User id: 980190962, name: "MyString", email: "MyString", created_at: "2016-11-28 13:29:24", updated_at: "2016-11-28 13:29:24">]>

いつの間にやらusersにレコードが出来上がっててちゃっかりemailは重複していたので全削除。
その後db:migrateしたら無事にmigrate出来ましたとさ。

4
1
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
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?