環境
macOS Catalina 10.15.7
Docker for mac 19.03.13
ruby on rails 6.0.3.4
mysql 5.7
前提・実現したいこと
rails db:migrate を行いたい。
普段開発しているコンテナで rails db:migrate した時に下記のように表示される。
ActiveRecord::PendingMigrationError Migrations are pending. To resolve this issue, run: rails db:migrate RAILS_ENV=development
発生している問題・エラーメッセージ
$ docker-compose down $ docker-compose up
コンテナを立ち上げてからコンテナを確認するとdb:migrateしてとのこと。
ActiveRecord::PendingMigrationError Migrations are pending. To resolve this issue, run: rails db:migrate RAILS_ENV=development
db:migrate を行うと
$ docker-compose exec web rails db:migrate
以下のようなエラーが出現。
% docker-compose exec web rails db:migrate == 20201102015615 CreatePostGs: migrating ===================================== -- create_table(:post_gs) rails aborted! StandardError: An error has occurred, all later migrations canceled: Mysql2::Error: Table 'mybest_development.post_gs' doesn't exist /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query' /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `block in query' /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `handle_interrupt' /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `query' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:201:in `block (2 levels) in execute' /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads' /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares' /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:200:in `block in execute' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract_adapter.rb:722:in `block (2 levels) in log' /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize' 〜中略〜 Caused by: ActiveRecord::StatementInvalid: Mysql2::Error: Table 'mybest_development.post_gs' doesn't exist /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query' /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `block in query' /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `handle_interrupt' /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `query' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:201:in `block (2 levels) in execute' /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads' /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares' /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads' 〜中略〜 /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:291:in `load_dependency' /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `require' /myapp/bin/rails:9:in `<top (required)>' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `load' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `call' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client/command.rb:7:in `call' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client.rb:30:in `run' /usr/local/bundle/gems/spring-2.1.1/bin/spring:49:in `<top (required)>' /usr/local/bundle/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `load' /usr/local/bundle/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `<top (required)>' /myapp/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)
試したこと
マイグレーション済みのマイグレーションファイルを確認。
$ docker-compose exec web rails db:migrate:status Status Migration ID Migration Name -------------------------------------------------- up 20201023085758 Devise create users up 20201024235341 Create active storage tablesactive storage up 20201026020750 Create post cs up 20201030080618 Create post bs down 20201102015615 Create post gs
mysqlにてschema_migrationsを確認。
mysql> select * from schema_migrations; +----------------+ | version | +----------------+ | 20201023085758 | | 20201024235341 | | 20201026020750 | | 20201030080618 | +----------------+ 4 rows in set (0.00 sec)
schema.rbを確認。
何故かマイグレーションしたいファイルの 20201102015615_create_post_gs の記述がある。
ActiveRecord::Schema.define(version: 2020_11_02_015615) do create_table "active_storage_attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false t.bigint "record_id", null: false t.bigint "blob_id", null: false t.datetime "created_at", null: false t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id" t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true end 〜中略〜 create_table "post_gs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| t.bigint "user_id" t.string "title", null: false t.string "place", null: false t.string "brand", null: false t.text "story", null: false t.text "evidence", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["user_id"], name: "index_post_gs_on_user_id" end
###追記
下記を追加で行いました。
・環境を指定してdb:migrateをしたところ、途中からエラーが発生しました。
$ docker-compose exec web rails db:migrate RAILS_ENV=development == 20201023085758 DeviseCreateUsers: migrating ================================ -- create_table(:users) -> 0.0416s -- add_index(:users, :email, {:unique=>true}) -> 0.0420s -- add_index(:users, :reset_password_token, {:unique=>true}) -> 0.0404s == 20201023085758 DeviseCreateUsers: migrated (0.1245s) ======================= == 20201024235341 CreateActiveStorageTables: migrating ======================== -- create_table(:active_storage_blobs, {}) -> 0.0409s -- create_table(:active_storage_attachments, {}) -> 0.0411s == 20201024235341 CreateActiveStorageTables: migrated (0.0821s) =============== == 20201026020750 CreatePostCs: migrating ===================================== -- create_table(:post_cs) -> 0.0455s == 20201026020750 CreatePostCs: migrated (0.0456s) ============================ == 20201030080618 CreatePostBs: migrating ===================================== -- create_table(:post_bs) rails aborted! StandardError: An error has occurred, all later migrations canceled: Mysql2::Error: Tablespace '`mybest_development`.`post_bs`' exists. /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query' /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `block in query' /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `handle_interrupt' /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `query' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:201:in `block (2 levels) in execute' /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads' /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares' /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:200:in `block in execute' 〜中略〜 /usr/local/bundle/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `load' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `call' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client/command.rb:7:in `call' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client.rb:30:in `run' /usr/local/bundle/gems/spring-2.1.1/bin/spring:49:in `<top (required)>' /usr/local/bundle/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `load' /usr/local/bundle/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `<top (required)>' /myapp/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)
11/03
20201030080618_create_post_bs.rb のソースコード記載しました。
20201030080618_create_post_bs.rb
1class CreatePostBs < ActiveRecord::Migration[6.0] 2 def change 3 create_table :post_bs do |t| 4 t.references :user,foreign_key: true 5 t.string :title,null: false 6 t.string :place,null: false 7 t.string :brand,null: false 8 t.text :story,null: false 9 t.text :evidence,null: false 10 t.timestamps 11 end 12 end 13end
コンテナを新しく建てても内容は変わらず・・・。
もしこの状況が理解できる方がいらっしゃいましたらぜひ教えて下さい!
回答1件
あなたの回答
tips
プレビュー