Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

6
5

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

[Rails]本日のエラー

Posted at

今度作業する時に、あれ?過去にも同じエラーが起こったことあるぞ!!となった時用に記事として保管しておくのが目的です。
なので、個人的に使用する記事なので投稿内容は大雑把にまとめます。

記事の流れ
1.なんの作業中にエラーが起きたのか
2.エラーの内容
3.対処法
4.原因

以上の流れで書いていきます。

#1.dbをリセットしようとした時

ターミナル.
rake db:migrate:reset

#2.エラー内容

ActiveRecord::NoEnvironmentInSchemaError: 

Environment data not found in the schema. To resolve this issue, run: 

        bin/rails db:environment:set RAILS_ENV=development


Tasks: TOP => db:migrate:reset => db:drop => db:check_protected_environments
(See full trace by running task with --trace)

#3.対処法

rake db:migrate:status

上記のコマンドでdbのステータスを確認します。
⬇︎実行内容

   up     20191223083031  Create articles
   up     20191223091431  Devise create users
   up     20191225043801  Add name to user
  down    20191225083536  Add frequency to habits

どうやら、一番下のマイグレーションファイルが怪しい。。。
確認したところ
habitsテーブルにfrequencyカラムを二回作成していました。。
なので、一番下のマイグレーションファイルを削除してみます。
※downの状態だったら、手動でファイルを削除しても問題ない
⬇︎実行結果

== 20191225234523 CreateHabits: migrating =====================================
-- create_table(:habits)
   -> 0.0302s
== 20191225234523 CreateHabits: migrated (0.0302s) ============================

== 20191229141445 AddImageToHabits: migrating =================================
-- add_column(:habits, :image, :string)
   -> 0.0195s
== 20191229141445 AddImageToHabits: migrated (0.0195s) ========================

== 20200101140707 AddDateAtToHabits: migrating ================================
-- add_column(:habits, :date_at, :date)
   -> 0.0231s
== 20200101140707 AddDateAtToHabits: migrated (0.0232s) =======================

== 20200103135302 CreateLikes: migrating ======================================
-- create_table(:likes)
   -> 0.0157s
== 20200103135302 CreateLikes: migrated (0.0158s) =============================

== 20200107020748 AddLikesCountToHabits: migrating ============================
-- add_column(:habits, :likes_count, :integer)
   -> 0.0174s
== 20200107020748 AddLikesCountToHabits: migrated (0.0174s) ===================

問題なくできました。

#4.原因
1つのテーブルに同じカラム名のカラムを追加、作成するマイグレーションファイルが重複していたため、migrateコマンドが実行できなかったと思われる。
なぜ二つ追加、作成したかは謎。
参考記事
https://blog.freedom-man.com/no-environment-in-schema-error

6
5
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

Qiita Advent Calendar is held!

Qiita Advent Calendar is an article posting event where you post articles by filling a calendar 🎅

Some calendars come with gifts and some gifts are drawn from all calendars 👀

Please tie the article to your calendar and let's enjoy Christmas together!

6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?