LoginSignup
2
4

More than 5 years have passed since last update.

Rails5.2でsqlite3からmysqlへの移行する方法

Last updated at Posted at 2019-02-10

若干詰まったところを先に書いておこう

なんか$ bundle exec rake db:migrateしたらエラーがたくさんでた。t.referencesが使えなかったり add_indexが使えなかったりした。db作る時にadd_indexされないとかなり困る。てことで、とりあえず、こんな感じで修正した。

変更前

db/migrate/20181002072214_create_microposts.rb
class CreateMicroposts < ActiveRecord::Migration[5.0]
  def change
    create_table :microposts do |t|
      t.text :content
      t.integer :user_id, foreign_key: true

      t.timestamps
    end
    add_index :microposts, [:user_id, :created_at], unique: true
  end
end

変更後

db/migrate/20181002072214_create_microposts.rb
class CreateMicroposts < ActiveRecord::Migration[5.0]
  def change
    create_table :microposts do |t|
      t.text :content
      t.integer :user_id, foreign_key: true

      t.timestamps
    end
    add_index :microposts, [:user_id, :created_at], unique: true
  end
end

あとは楽勝だった

config/database.yml

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  adapter: mysql2
  encoding: utf8
  database: test13_development
  pool: 5
  username: root
  password:
  socket: /tmp/mysql.sock

test:
  adapter: mysql2
  encoding: utf8
  database: test13_test
  pool: 5
  username: root
  password:
  socket: /tmp/mysql.sock

production:
  <<: *default
  database: db/production.sqlite3

gemファイルも直す

group :development, :test do
  #gem 'sqlite3', '1.3.13'
   gem 'mysql2'

出現したエラー

rake aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Table 'lang_sns10_development.user' doesn't exist: CREATE UNIQUE INDEX `my_index`  ON `user` (`user_id`, `created_at`) 
/Users/ryosuke/Desktop/rails-project-we-are/db/migrate/20181002072214_create_microposts.rb:9:in `change'
/Users/ryosuke/.rbenv/versions/2.5.1/bin/bundle:23:in `load'
/Users/ryosuke/.rbenv/versions/2.5.1/bin/bundle:23:in `<main>'

Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'lang_sns10_development.user' doesn't exist: CREATE UNIQUE INDEX `my_index`  ON `user` (`user_id`, `created_at`) 
/Users/ryosuke/Desktop/rails-project-we-are/db/migrate/20181002072214_create_microposts.rb:9:in `change'
/Users/ryosuke/.rbenv/versions/2.5.1/bin/bundle:23:in `load'
/Users/ryosuke/.rbenv/versions/2.5.1/bin/bundle:23:in
 `<main>'

-- create_table(:microposts, {:options=>"ENGINE=InnoDB", :id=>:integer})
   -> 0.0141s
-- add_index(:microposts, ["user_id", "created_at"], {:unique=>true})
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Key column 'user_id' doesn't exist in table: CREATE UNIQUE INDEX `index_microposts_on_user_id_and_created_at`  ON `microposts` (`user_id`, `created_at`) 
/Users/ryosuke/Desktop/rails-project-we-are/db/migrate/20181002072214_create_microposts.rb:10:in `change'
/Users/ryosuke/.rbenv/versions/2.5.1/bin/bundle:23:in `load'
/Users/ryosuke/.rbenv/versions/2.5.1/bin/bundle:23:in `<main>'

Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: Key column 'user_id' doesn't exist in table: CREATE UNIQUE INDEX `index_microposts_on_user_id_and_created_at`  ON `microposts` (`user_id`, `created_at`) 
/Users/ryosuke/Desktop/rails-project-we-are/db/migrate/20181002072214_create_microposts.rb:10:in `change'
/Users/ryosuke/.rbenv/versions/2.5.1/bin/bundle:23:in `load'
/Users/ryosuke/.rbenv/versions/2.5.1/bin/bundle:23:in `<main>'
2
4
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
2
4