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

【Ruby】テーブルに対しての設定値・備忘録

Posted at

date型のカラムを追加しようとしてエラーが出てしまった話。

% rails db:migrate

== 20201220042102 DeviseCreateUsers: migrating ================================
-- create_table(:users)
rails aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Invalid default value for 'birthday'
/Users/bon/projects/furima-32411/db/migrate/20201220042102_devise_create_users.rb:5:in `change'
/Users/bon/projects/furima-32411/bin/rails:9:in `<top (required)>'
/Users/bon/projects/furima-32411/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: Invalid default value for 'birthday'
/Users/bon/projects/furima-32411/db/migrate/20201220042102_devise_create_users.rb:5:in `change'
/Users/bon/projects/furima-32411/bin/rails:9:in `<top (required)>'
/Users/bon/projects/furima-32411/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
Mysql2::Error: Invalid default value for 'birthday'
/Users/bon/projects/furima-32411/db/migrate/20201220042102_devise_create_users.rb:5:in `change'
/Users/bon/projects/furima-32411/bin/rails:9:in `<top (required)>'
/Users/bon/projects/furima-32411/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

なんか前もこんなことあったような・・・
birthdayにはdate型を指定していたがエラー文からして以前やったものに近い気がしていた。
色々と調べたところやはりdate型はdefault値で空が設定されており、
default: ""」の設定が不要らしい。
マイグレーションファイルから記述を削除して再度マイグレート、成功。


t.date :birthday, null: false   #←ここに「 default: "" 」があったためエラーに

動的値と静的値、ややこしい。

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?