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.

herokuデプロイ Mysql2::Error: Table 'users' already exists: CREATE TABLE `users`

Last updated at Posted at 2020-07-19

#解決策

heroku run rake db:migrate RAILS_ENV=production
エラー

heroku run rake db:reset RAILS_ENV=production

heroku run rake db:reset RAILS_ENV=production DISABLE_DATABASE_ENVIRONMENT_CHECK=1

asatokensei@MacBook-Air memopy % heroku run rake db:migrate RAILS_ENV=production                        
Running rake db:migrate RAILS_ENV=production on ⬢ memopy... up, run.8417 (Free)
/app/vendor/bundle/ruby/2.7.0/gems/actionpack-5.2.4.3/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/actionpack-5.2.4.3/lib/action_dispatch/middleware/static.rb:111: warning: The called method `initialize' is defined here
D, [2020-07-19T01:46:02.234093 #4] DEBUG -- :    (10.4ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
D, [2020-07-19T01:46:02.309138 #4] DEBUG -- :    (7.4ms)  SELECT GET_LOCK('3493520402598712500', 0)
D, [2020-07-19T01:46:02.343777 #4] DEBUG -- :    (4.5ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
I, [2020-07-19T01:46:02.355197 #4]  INFO -- : Migrating to CreateUsers (20200622082810)
== 20200622082810 CreateUsers: migrating ======================================
-- create_table(:users)
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/mysql/schema_statements.rb:78: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/schema_definitions.rb:263: warning: The called method `initialize' is defined here
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:301: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/schema_definitions.rb:204: warning: The called method `primary_key' is defined here
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/schema_definitions.rb:366: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/mysql/schema_definitions.rb:55: warning: The called method `new_column_definition' is defined here
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/schema_creation.rb:19: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:448: warning: The called method `type_to_sql' is defined here
D, [2020-07-19T01:46:02.394757 #4] DEBUG -- :    (13.5ms)  CREATE TABLE `users` (`id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` varchar(255), `email` varchar(255), `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL)
D, [2020-07-19T01:46:02.404327 #4] DEBUG -- :    (8.3ms)  SELECT RELEASE_LOCK('3493520402598712500')
rake 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, `name` varchar(255), `email` varchar(255), `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL)
~

Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'users' already exists: CREATE TABLE `users` (`id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` varchar(255), `email` varchar(255), `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL)
/app/vendor/bundle/ruby/2.7.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query'
~

Caused by:
Mysql2::Error: Table 'users' already exists
/app/vendor/bundle/ruby/2.7.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query'
~

/app/bin/bundle:3:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
asatokensei@MacBook-Air memopy % heroku run rake db:reset RAILS_ENV=production
Running rake db:reset RAILS_ENV=production on ⬢ memopy... up, run.6452 (Free)
/app/vendor/bundle/ruby/2.7.0/gems/actionpack-5.2.4.3/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/actionpack-5.2.4.3/lib/action_dispatch/middleware/static.rb:111: warning: The called method `initialize' is defined here
D, [2020-07-19T01:46:43.752622 #4] DEBUG -- :    (3.5ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
D, [2020-07-19T01:46:43.810885 #4] DEBUG -- :    (2.8ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
D, [2020-07-19T01:46:43.826800 #4] DEBUG -- :    (2.9ms)  SELECT `ar_internal_metadata`.`value` FROM `ar_internal_metadata` WHERE `ar_internal_metadata`.`key` = 'environment'
D, [2020-07-19T01:46:43.834609 #4] DEBUG -- :    (4.2ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
D, [2020-07-19T01:46:43.840994 #4] DEBUG -- :    (2.9ms)  SELECT `ar_internal_metadata`.`value` FROM `ar_internal_metadata` WHERE `ar_internal_metadata`.`key` = 'environment'
D, [2020-07-19T01:46:43.847339 #4] DEBUG -- :    (2.9ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
D, [2020-07-19T01:46:43.858665 #4] DEBUG -- :    (2.9ms)  SELECT `ar_internal_metadata`.`value` FROM `ar_internal_metadata` WHERE `ar_internal_metadata`.`key` = 'environment'
rake aborted!
ActiveRecord::ProtectedEnvironmentError: You are attempting to run a destructive action against your 'production' database.
If you are sure you want to continue, run the same command with the environment variable:
DISABLE_DATABASE_ENVIRONMENT_CHECK=1
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/tasks/database_tasks.rb:61:in `check_protected_environments!'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/railties/databases.rake:13:in `block (2 levels) in <main>'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/app/bin/bundle:3:in `load'
/app/bin/bundle:3:in `<main>'
Tasks: TOP => db:reset => db:drop => db:check_protected_environments
(See full trace by running task with --trace)

DISABLE_DATABASE_ENVIRONMENT_CHECK=1
を環境変数につけろと怒られる

asatokensei@MacBook-Air memopy % heroku run rake db:reset RAILS_ENV=production DISABLE_DATABASE_ENVIRONMENT_CHECK=1
Running rake db:reset RAILS_ENV=production DISABLE_DATABASE_ENVIRONMENT_CHECK=1 on ⬢ memopy... up, run.1003 (Free)
/app/vendor/bundle/ruby/2.7.0/gems/actionpack-5.2.4.3/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/actionpack-5.2.4.3/lib/action_dispatch/middleware/static.rb:111: warning: The called method `initialize' is defined here
D, [2020-07-19T01:47:19.813454 #4] DEBUG -- :    (2.4ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
D, [2020-07-19T01:47:19.845868 #4] DEBUG -- :    (29.8ms)  DROP DATABASE IF EXISTS `heroku_a8b1a434e18a217`
Dropped database 'heroku_a8b1a434e18a217'
D, [2020-07-19T01:47:19.888807 #4] DEBUG -- :    (3.6ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
D, [2020-07-19T01:47:19.897784 #4] DEBUG -- :    (5.6ms)  CREATE DATABASE `heroku_a8b1a434e18a217` DEFAULT CHARACTER SET `utf8`
Created database 'heroku_a8b1a434e18a217'
-- create_table("friends", {:options=>"ENGINE=InnoDB DEFAULT CHARSET=utf8", :force=>:cascade})
D, [2020-07-19T01:47:19.941421 #4] DEBUG -- :    (3.3ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/migration.rb:871: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:290: warning: The called method `create_table' is defined here
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/mysql/schema_statements.rb:78: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/schema_definitions.rb:263: warning: The called method `initialize' is defined here
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:301: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/schema_definitions.rb:204: warning: The called method `primary_key' is defined here
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/schema_definitions.rb:366: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/mysql/schema_definitions.rb:55: warning: The called method `new_column_definition' is defined here
D, [2020-07-19T01:47:19.950134 #4] DEBUG -- :    (5.0ms)  DROP TABLE IF EXISTS `friends` CASCADE
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/schema_creation.rb:19: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:448: warning: The called method `type_to_sql' is defined here
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/mysql/schema_creation.rb:67: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:1135: warning: The called method `add_index_options' is defined here
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:1162: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:1215: warning: The called method `quoted_columns_for_index' is defined here
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:1219: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/mysql/schema_statements.rb:116: warning: The called method `add_options_for_index_columns' is defined here
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/mysql/schema_statements.rb:117: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/mysql/schema_statements.rb:109: warning: The called method `add_index_length' is defined here
D, [2020-07-19T01:47:19.975793 #4] DEBUG -- :    (21.7ms)  CREATE TABLE `friends` (`id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` varchar(255), `user_id` bigint, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL,  INDEX `index_friends_on_user_id`  (`user_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8
   -> 0.0745s
-- create_table("friends_details", {:options=>"ENGINE=InnoDB DEFAULT CHARSET=utf8", :force=>:cascade})
D, [2020-07-19T01:47:19.981422 #4] DEBUG -- :    (5.1ms)  DROP TABLE IF EXISTS `friends_details` CASCADE
D, [2020-07-19T01:47:20.004336 #4] DEBUG -- :    (18.6ms)  CREATE TABLE `friends_details` (`id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, `feature` varchar(255), `content` varchar(255), `friend_id` bigint, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL,  INDEX `index_friends_details_on_friend_id`  (`friend_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8
   -> 0.0285s
-- create_table("users", {:options=>"ENGINE=InnoDB DEFAULT CHARSET=utf8", :force=>:cascade})
D, [2020-07-19T01:47:20.009874 #4] DEBUG -- :    (4.9ms)  DROP TABLE IF EXISTS `users` CASCADE
D, [2020-07-19T01:47:20.026343 #4] DEBUG -- :    (16.0ms)  CREATE TABLE `users` (`id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` varchar(255), `email` varchar(255), `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, `password_digest` varchar(255), `remember_digest` varchar(255)) ENGINE=InnoDB DEFAULT CHARSET=utf8
   -> 0.0219s
-- add_foreign_key("friends", "users")
D, [2020-07-19T01:47:20.051596 #4] DEBUG -- :    (24.5ms)  ALTER TABLE `friends` ADD CONSTRAINT `fk_rails_9cfeeb4593`
FOREIGN KEY (`user_id`)
  REFERENCES `users` (`id`)

   -> 0.0252s
-- add_foreign_key("friends_details", "friends")
D, [2020-07-19T01:47:20.074413 #4] DEBUG -- :    (22.1ms)  ALTER TABLE `friends_details` ADD CONSTRAINT `fk_rails_9652152079`
FOREIGN KEY (`friend_id`)
  REFERENCES `friends` (`id`)

   -> 0.0227s
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/schema_migration.rb:30: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/schema_definitions.rb:231: warning: The called method `string' is defined here
D, [2020-07-19T01:47:20.141107 #4] DEBUG -- :    (18.0ms)  CREATE TABLE `schema_migrations` (`version` varchar(255) NOT NULL PRIMARY KEY)
D, [2020-07-19T01:47:20.164256 #4] DEBUG -- :    (3.7ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
D, [2020-07-19T01:47:20.181513 #4] DEBUG -- :    (11.7ms)  INSERT INTO `schema_migrations` (version) VALUES (20200703094957)
D, [2020-07-19T01:47:20.195092 #4] DEBUG -- :    (13.2ms)  INSERT INTO `schema_migrations` (version) VALUES
(20200622082810),
(20200625044306),
(20200628063636),
(20200622105712);


/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/internal_metadata.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/schema_definitions.rb:231: warning: The called method `string' is defined here
D, [2020-07-19T01:47:20.221338 #4] DEBUG -- :    (16.8ms)  CREATE TABLE `ar_internal_metadata` (`key` varchar(255) NOT NULL PRIMARY KEY, `value` varchar(255), `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL)
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/mysql/database_statements.rb:12: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/query_cache.rb:95: warning: The called method `select_all' is defined here
D, [2020-07-19T01:47:20.235618 #4] DEBUG -- :   ActiveRecord::InternalMetadata Load (3.8ms)  SELECT  `ar_internal_metadata`.* FROM `ar_internal_metadata` WHERE `ar_internal_metadata`.`key` = 'environment' LIMIT 1
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/transactions.rb:212: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/database_statements.rb:260: warning: The called method `transaction' is defined here
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/transaction.rb:171: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/transaction.rb:97: warning: The called method `initialize' is defined here
D, [2020-07-19T01:47:20.282497 #4] DEBUG -- :    (3.6ms)  BEGIN
D, [2020-07-19T01:47:20.287857 #4] DEBUG -- :   ActiveRecord::InternalMetadata Create (3.4ms)  INSERT INTO `ar_internal_metadata` (`key`, `value`, `created_at`, `updated_at`) VALUES ('environment', 'production', '2020-07-19 01:47:20', '2020-07-19 01:47:20')
D, [2020-07-19T01:47:20.297353 #4] DEBUG -- :    (9.1ms)  COMMIT
D, [2020-07-19T01:47:20.305087 #4] DEBUG -- :   ActiveRecord::InternalMetadata Load (3.5ms)  SELECT  `ar_internal_metadata`.* FROM `ar_internal_metadata` WHERE `ar_internal_metadata`.`key` = 'environment' LIMIT 1
D, [2020-07-19T01:47:20.309743 #4] DEBUG -- :    (3.2ms)  BEGIN
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/persistence.rb:705: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-5.2.4.3/lib/active_record/timestamp.rb:105: warning: The called method `_update_record' is defined here
D, [2020-07-19T01:47:20.317225 #4] DEBUG -- :    (3.3ms)  COMMIT
D, [2020-07-19T01:47:20.386050 #4] DEBUG -- :    (3.4ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
D, [2020-07-19T01:47:20.408582 #4] DEBUG -- :    (4.1ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC

#参考記事
https://qiita.com/Atsushi_/items/a230fb7f624d1eebf2f3

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?