今、現在フォロワー機能を導入しようと考えているのですが、rake db:migrateをterminalで実行時にエラーにあってしまい、抜け出すことができない状態にあります。
参考サイト
・フォロワー機能(このサイト通りにやったのですが、最初の段階でエラーになってしまいました.)⤵️
: https://qiita.com/mitsumitsu1128/items/e41e2ff37f143db81897
・already exitst解決方法: https://bokunonikki.net/post/2018/0214_rails_mysql_error/
・https://qiita.com/okamoto_ryo/items/8aceacdd81d079e8a120
1回目にrake db:migrateをした時のエラー
terminal
1$ rake db:migrate 2== 20200721090023 CreateRelationships: migrating ============================== 3-- create_table(:relationships) 4rake aborted! 5StandardError: An error has occurred, all later migrations canceled: 6 7Mysql2::Error: Cannot add foreign key constraint: ALTER TABLE `relationships` ADD CONSTRAINT `fk_rails_be1f05b3d7` 8FOREIGN KEY (`follow_id`) 9 REFERENCES `follows` (`id`) 10/Users/hiroshi/wa/db/migrate/20200721090023_create_relationships.rb:3:in `change' 11 12Caused by: 13ActiveRecord::StatementInvalid: Mysql2::Error: Cannot add foreign key constraint: ALTER TABLE `relationships` ADD CONSTRAINT `fk_rails_be1f05b3d7` 14FOREIGN KEY (`follow_id`) 15 REFERENCES `follows` (`id`) 16/Users/hiroshi/wa/db/migrate/20200721090023_create_relationships.rb:3:in `change' 17 18Caused by: 19Mysql2::Error: Cannot add foreign key constraint 20/Users/hiroshi/wa/db/migrate/20200721090023_create_relationships.rb:3:in `change' 21Tasks: TOP => db:migrate 22(See full trace by running task with --trace) 23
terminalでrails sをした時に、'bin/rake db:migrate RAILS_ENV=development'が表示され、打ち込むとこのエラーが表示されました。
terminal
1$ bin/rake db:migrate RAILS_ENV=development 2Running via Spring preloader in process 61687 3== 20200721090023 CreateRelationships: migrating ============================== 4-- create_table(:relationships) 5rake aborted! 6StandardError: An error has occurred, all later migrations canceled: 7 8Mysql2::Error: Table 'relationships' already exists: CREATE TABLE `relationships` (`id` int(11) auto_increment PRIMARY KEY, `user_id` int(11), `follow_id` int(11), `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL) ENGINE=InnoDB 9/Users/hiroshi/wa/db/migrate/20200721090023_create_relationships.rb:3:in `change' 10-e:1:in `<main>' 11 12Caused by: 13ActiveRecord::StatementInvalid: Mysql2::Error: Table 'relationships' already exists: CREATE TABLE `relationships` (`id` int(11) auto_increment PRIMARY KEY, `user_id` int(11), `follow_id` int(11), `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL) ENGINE=InnoDB 14/Users/hiroshi/wa/db/migrate/20200721090023_create_relationships.rb:3:in `change' 15-e:1:in `<main>' 16 17Caused by: 18Mysql2::Error: Table 'relationships' already exists 19/Users/hiroshi/wa/db/migrate/20200721090023_create_relationships.rb:3:in `change' 20-e:1:in `<main>' 21Tasks: TOP => db:migrate 22(See full trace by running task with --trace) 23
db
1class CreateRelationships < ActiveRecord::Migration 2 def change 3 create_table :relationships do |t| 4 t.references :user, foreign_key: true 5 t.references :follow, foreign_key: { to_table: :users } 6 7 t.timestamps 8 end 9 add_index :relationships, [:user_id, :follow_id], :unique => true 10 11 end 12end
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。