0
0

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 1 year has passed since last update.

rails db:migrateのエラーについて(StandardError: An error has occurred, all later migrations canceled:)

Posted at

はじめに

エラーの対処法メモ。

== 20221226020652 DeviseCreateUsers: migrating ================================
-- create_table(:users)
rails aborted!
StandardError: An error has occurred, all later migrations canceled:
以下、続く

この記述はエラーが出たのでマイグレーションをキャンセルしたという記述。
この後の記述でエラーの詳細を見ます。

原因

                略
Mysql2::Error: Invalid default value for 'hoge'
                略
ActiveRecord::StatementInvalid: Mysql2::Error: Invalid default value for 'hoge'
                略
Mysql2::Error: Invalid default value for 'hoge'

デフォルトの値が無効だとのこと。

t.date :hoge,      null: false, default: ""

公式によるとdate型はMySQL は、DATE 値を'YYYY-MM-DD'形式で取得して表示します。とあるのでdefault:""が原因でした。
以下、公式
11.2.2 DATE、DATETIME、および TIMESTAMP 型

解決策

t.date :hoge,      null: false

default:""を消してから rails db:migrate

== 20221226020652 DeviseCreateUsers: migrating ================================
-- create_table(:users)
   -> 0.0527s
        略
== 20221226020652 DeviseCreateUsers: migrated (0.0799s) =======================
 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20221224075426  Create hoges
   up     20221226020652  Devise create users

無事にマイグレーションファイルが設定されました。

終わりに

カラム型:dateは初めて使うのでうっかりしてました。
db:resetなど余計なことをしましたが、しっかりエラー文を読んでいこうと思います。

参考サイト
https://qiita.com/yuya_maru2/items/b839af0b60d8096ecf49

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?