LoginSignup
7
0

More than 5 years have passed since last update.

時間を取り入れたFixtureとデータベースに対するエラー対処

Last updated at Posted at 2016-06-01

経緯

  1. fixtureに時間を取り入れたデータを投入.
  2. テストを実行する.
  3. 全てのテストに対してエラーを吐く

エラー内容

stack_trace
ActiveRecord::StatementInvalid: Mysql2::Error: Incorrect datetime value: '2016-06-01 07:11:00 UTC' for column 'created_at' at row 1: INSERT INTO `microposts` (`content`, `created_at`, `updated_at`, `id`) VALUES ('I just ate an orange!', '2016-06-01 07:11:00 UTC', '2016-06-01 07:21:00', 499495288)

datetimeがおかしいとエラーを吐いているので,datetimeを見てみると...
データベースのタイムゾーンがUTCになっている.

日本とUTCのは違う(当たり前)のでデータベース内でエラーが起こっていることがわかった.

対策

データベース内のタイムゾーンとアプリケーションのタイムゾーンを変更する.

config/application.rb
module アプリ名
    class Application < Rails::Application
.
.
.
        config.time_zone = 'Tokyo' #アプリケーションのタイムゾーン
        config.active_record.default_timezone = :local #データベースのタイムゾーン
.
.
.
    end
end

この後実行するとちゃんとテストが通るようになった.

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