質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

1回答

4172閲覧

usersテーブルがalready existsとなってマイグレーションできない

kai20000803

総合スコア29

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/08/13 05:50

編集2020/08/13 06:36

自分がしたいこと

ActiveRecord::PendingMigrationError
Migrations are pending. To resolve this issue, run: bin/rails db:migrate RAILS_ENV=development

このエラーを解消したいです。

コード ```### 自分が考えたこと [リンク内容](https://qiita.com/kanuu/items/a9223712ee0ff8d19d56) この記事を参考にして すでに存在しているusersを消してやろうと思ったのですが、 これをしてみると == 20200813043854 CreateUsers: migrating ====================================== -- create_table(:users) rails aborted! StandardError: An error has occurred, all later migrations canceled: Mysql2::Error: Table 'users' already exists: CREATE TABLE `users` (`id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL) /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `block in query' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `handle_interrupt' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `query' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:187:in `block (2 levels) in execute' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.4.3/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.4.3/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.4.3/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:186:in `block in execute' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract_adapter.rb:581:in `block (2 levels) in log' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract_adapter.rb:580:in `block in log' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.4.3/lib/active_support/notifications/instrumenter.rb:23:in `instrument' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract_adapter.rb:571:in `log' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:185:in `execute' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/mysql/database_statements.rb:28:in `execute' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:311:in `create_table' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:871:in `block in method_missing' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:840:in `block in say_with_time' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:840:in `say_with_time' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:860:in `method_missing' /home/ec2-user/environment/goodbooks/db/migrate/20200813043854_create_users.rb:3:in `change' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:814:in `exec_migration' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:798:in `block (2 levels) in migrate' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:797:in `block in migrate' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:416:in `with_connection' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:796:in `migrate' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:977:in `migrate' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:1292:in `block in execute_migration_in_transaction' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:1345:in `ddl_transaction' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:1291:in `execute_migration_in_transaction' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:1263:in `block in migrate_without_lock' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:1262:in `each' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:1262:in `migrate_without_lock' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:1210:in `block in migrate' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:1363:in `with_advisory_lock' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:1210:in `migrate' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:1036:in `up' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:1011:in `migrate' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/tasks/database_tasks.rb:172:in `migrate' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.3/lib/active_record/railties/databases.rake:60:in `block (2 levels) in <top (required)>' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/railties-5.2.4.3/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/railties-5.2.4.3/lib/rails/commands/rake/rake_command.rb:20:in `perform' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/railties-5.2.4.3/lib/rails/command.rb:48:in `invoke' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/railties-5.2.4.3/lib/rails/commands.rb:18:in `<top (required)>' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:291:in `block in require' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:257:in `load_dependency' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:291:in `require' /home/ec2-user/environment/goodbooks/bin/rails:9:in `<top (required)>' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/spring-2.1.0/lib/spring/client.rb:30:in `run' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/spring-2.1.0/bin/spring:49:in `<top (required)>' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load' /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>' /home/ec2-user/environment/goodbooks/bin/spring:15:in `require' /home/ec2-user/environment/goodbooks/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' というようにusersがalready existなのでとなりできませんでした。 これについての解決策を教えてください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

  1. 既に有るのになぜ create する必要があるのでしょう?
  2. 「すでに存在しているusersを消してやろうと思ったのですが、

これをしてみると」
とあるのですが、migrationの中身は消すというものでは無いですね

何のために users tableを削除しなければならなくなったのか?
それが最適解かどうかから見てみましょう

そのためにusers tableを削除する というのが正しい?

1, rails db:migrate:status
の結果を載せてください。長ければ、「最初にでてくる down」 の2行ぐらい前からでよいです。

  1. downが出ているmigrationの中で一番最初に出ているものを載せてください

投稿2020/08/13 06:10

編集2020/08/13 06:43
winterboum

総合スコア23347

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kai20000803

2020/08/13 06:35

ActiveRecord::PendingMigrationError Migrations are pending. To resolve this issue, run: bin/rails db:migrate RAILS_ENV=development Extracted source (around line #579): 577 578 579 580 581 582 # Raises <tt>ActiveRecord::PendingMigrationError</tt> error if any migrations are pending. def check_pending!(connection = Base.connection) raise ActiveRecord::PendingMigrationError if connection.migration_context.needs_migration? end def load_schema_if_pending! Rails.root: /home/ec2-user/environment/goodbooks Application Trace | Framework Trace | Full Trace activerecord (5.2.4.3) lib/active_record/migration.rb:579:in `check_pending!' activerecord (5.2.4.3) lib/active_record/migration.rb:556:in `call' actionpack (5.2.4.3) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call' activesupport (5.2.4.3) lib/active_support/callbacks.rb:98:in `run_callbacks' actionpack (5.2.4.3) lib/action_dispatch/middleware/callbacks.rb:26:in `call' actionpack (5.2.4.3) lib/action_dispatch/middleware/executor.rb:14:in `call' actionpack (5.2.4.3) lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call' web-console (3.7.0) lib/web_console/middleware.rb:135:in `call_app' web-console (3.7.0) lib/web_console/middleware.rb:22:in `block in call' web-console (3.7.0) lib/web_console/middleware.rb:20:in `catch' web-console (3.7.0) lib/web_console/middleware.rb:20:in `call' actionpack (5.2.4.3) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call' railties (5.2.4.3) lib/rails/rack/logger.rb:38:in `call_app' railties (5.2.4.3) lib/rails/rack/logger.rb:26:in `block in call' activesupport (5.2.4.3) lib/active_support/tagged_logging.rb:71:in `block in tagged' activesupport (5.2.4.3) lib/active_support/tagged_logging.rb:28:in `tagged' activesupport (5.2.4.3) lib/active_support/tagged_logging.rb:71:in `tagged' railties (5.2.4.3) lib/rails/rack/logger.rb:26:in `call' sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call' actionpack (5.2.4.3) lib/action_dispatch/middleware/remote_ip.rb:81:in `call' actionpack (5.2.4.3) lib/action_dispatch/middleware/request_id.rb:27:in `call' rack (2.2.3) lib/rack/method_override.rb:24:in `call' rack (2.2.3) lib/rack/runtime.rb:22:in `call' activesupport (5.2.4.3) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call' actionpack (5.2.4.3) lib/action_dispatch/middleware/executor.rb:14:in `call' actionpack (5.2.4.3) lib/action_dispatch/middleware/static.rb:127:in `call' rack (2.2.3) lib/rack/sendfile.rb:110:in `call' railties (5.2.4.3) lib/rails/engine.rb:524:in `call' puma (3.12.6) lib/puma/configuration.rb:227:in `call' puma (3.12.6) lib/puma/server.rb:706:in `handle_request' puma (3.12.6) lib/puma/server.rb:476:in `process_client' puma (3.12.6) lib/puma/server.rb:334:in `block in run' puma (3.12.6) lib/puma/thread_pool.rb:135:in `block in spawn_thread' Request Parameters: None Toggle session dump Toggle env dump Response Headers: None すいません、これのエラー解消のためです。載せ忘れてました。 質問の方にも載せておきます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問