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

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

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

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Ruby on Rails

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

Q&A

解決済

1回答

4451閲覧

Ruby on Rails にてマイグレーションができない。

frusciante

総合スコア8

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Ruby on Rails

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

0グッド

0クリップ

投稿2020/02/06 11:46

前提・実現したいこと

現在、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

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

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

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

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

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

guest

回答1

0

自己解決

以下の記事を参考に、マイグレーションファイルのバージョンを"5.2"から"5.0"に修正する事で解消しました。
参考記事①
参考記事②

投稿2020/02/06 17:53

frusciante

総合スコア8

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問