前提・実現したいこと
現在、Ruby on Rails にてアプリ開発中ですが、マイグレーション時に外部キーのエラーが起きているようで、マイグレーションができません。
※mysql2のgemをインストールしています。
※user_idの外部キーをもつテーブルは作成できているのですが、user_id以外の外部キーをもつテーブルが作成されません。
お手数ですが、ご助言いただけますと幸いでございます。
発生している問題・エラーメッセージ
- ターミナルのエラー表示
StandardError: An error has occurred, all later migrations canceled: Column `musician_id` on table `matters` does not match column `id` on `musicians`, which has type `bigint(20)`. To resolve this issue, change the type of the `musician_id` column on `matters` to be :bigint. (For example `t.bigint :musician_id`). Original message: Mysql2::Error: Cannot add foreign key constraint: CREATE TABLE `matters` (`id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, `title` varchar(255) NOT NULL, `reward` int NOT NULL, `deadline` date NOT NULL, `start` date, `end` date NOT NULL, `content` text NOT NULL, `payment` varchar(255), `supplement` text, `status` int DEFAULT 0 NOT NULL, `musician_id` bigint, `matter_category_id` bigint NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, INDEX `index_matters_on_musician_id` (`musician_id`), INDEX `index_matters_on_matter_category_id` (`matter_category_id`), CONSTRAINT `fk_rails_f1238488ae` FOREIGN KEY (`musician_id`) REFERENCES `musicians` (`id`) , CONSTRAINT `fk_rails_81193a316a` FOREIGN KEY (`matter_category_id`) REFERENCES `matter_categories` (`id`) ) /Users/owner/projects/music-works/db/migrate/20200206092137_create_matters.rb:3:in `change' /Users/owner/projects/music-works/bin/rails:9:in `<top (required)>' /Users/owner/projects/music-works/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: ActiveRecord::MismatchedForeignKey: Column `musician_id` on table `matters` does not match column `id` on `musicians`, which has type `bigint(20)`. To resolve this issue, change the type of the `musician_id` column on `matters` to be :bigint. (For example `t.bigint :musician_id`). Original message: Mysql2::Error: Cannot add foreign key constraint: CREATE TABLE `matters` (`id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, `title` varchar(255) NOT NULL, `reward` int NOT NULL, `deadline` date NOT NULL, `start` date, `end` date NOT NULL, `content` text NOT NULL, `payment` varchar(255), `supplement` text, `status` int DEFAULT 0 NOT NULL, `musician_id` bigint, `matter_category_id` bigint NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, INDEX `index_matters_on_musician_id` (`musician_id`), INDEX `index_matters_on_matter_category_id` (`matter_category_id`), CONSTRAINT `fk_rails_f1238488ae` FOREIGN KEY (`musician_id`) REFERENCES `musicians` (`id`) , CONSTRAINT `fk_rails_81193a316a` FOREIGN KEY (`matter_category_id`) REFERENCES `matter_categories` (`id`) ) /Users/owner/projects/music-works/db/migrate/20200206092137_create_matters.rb:3:in `change' /Users/owner/projects/music-works/bin/rails:9:in `<top (required)>' /Users/owner/projects/music-works/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: Mysql2::Error: Cannot add foreign key constraint /Users/owner/projects/music-works/db/migrate/20200206092137_create_matters.rb:3:in `change' /Users/owner/projects/music-works/bin/rails:9:in `<top (required)>' /Users/owner/projects/music-works/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)
該当のソースコード
- 20200206092137_create_matters.rb
class CreateMatters < ActiveRecord::Migration[5.2] def change create_table :matters do |t| t.string :title, null: false t.integer :reward, null: false t.date :deadline, null: false t.date :start t.date :end, null: false t.text :content, null: false t.string :payment t.text :supplement t.integer :status, null: false, default: "0" t.references :musician, type: :bigint, foreign_key: true t.references :matter_category, null: false, foreign_key: true t.timestamps end end end
- 20200206092054_create_musicians.rb
class CreateMusicians < ActiveRecord::Migration[5.2] def change create_table :musicians do |t| t.text :image t.string :name, null: false t.text :biography, null: false t.text :activity_history, null: false t.text :activity_place, null: false t.string :email, null: false t.text :sound_source t.text :homepage t.text :twitter t.text :facebook t.text :other_link t.text :live_info t.string :office t.references :user, null: false, foreign_key: true t.timestamps end end end
※musiciansテーブル自体は作成できていますが、mattersテーブルが作成できていないです。
試したこと
自分なりにエラー文を読んでみたところ、外部キー(musician_id
on table matters
)のデータ型を変更する必要があると思い以下の様にマイグレーションファイル(20200206092137_create_matters.rb)を修正しました。
※修正前に以下の記事を参考にしております。
参考記事①
参考記事②
- 修正前
t.references :musician, null: false, foreign_key: true
- 修正後
t.references :musician, type: :bigint, foreign_key: true
改めて、マイグレーションを実行してみたのですが、質問の冒頭に書いたものと同様のエラー文が表示されてしまい意図したテーブルが作成されませんでした。
開発環境
- ruby 2.5.1
- Rails 5.2.4.1
- mysql Ver 14.14 Distrib 5.6.43, for osx10.14
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。