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

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

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

CentOSは、主にRed Hat Enterprise Linux(RHEL)をベースにした、フリーのソフトウェアオペレーティングシステムです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

MySQL

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

Ruby on Rails

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

Vagrant

Vagrantは、VirtualBox上の仮想マシンを コマンドラインから作成してくれるソフトウェアです。 ビルド環境など容易に構築が可能です。

Q&A

解決済

1回答

3704閲覧

Rails5.1.3でマイグレーションを実行時のエラー解消方法を教えてください。

koume

総合スコア458

CentOS

CentOSは、主にRed Hat Enterprise Linux(RHEL)をベースにした、フリーのソフトウェアオペレーティングシステムです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

MySQL

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

Ruby on Rails

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

Vagrant

Vagrantは、VirtualBox上の仮想マシンを コマンドラインから作成してくれるソフトウェアです。 ビルド環境など容易に構築が可能です。

0グッド

0クリップ

投稿2017/08/27 03:52

編集2017/08/27 05:25

以下の環境でWebアプリケーション制作の勉強中です。新たにテーブルを作成し(messagesテーブル)マイグレーションスクリプトを
作成し、マイグレーションを実行するとエラーが出てしまい解決できなくて困っております。
環境およびコード、エラー内容は以下になります。

環境
Ruby 2.3.1
Rails5.1.3
MySQL5.6.37

マイグレーションスクリプト

class CreateMessages < ActiveRecord::Migration[5.1] def change create_table :messages do |t| t.references :customer, null: false # 顧客への外部キー t.references :staff_member # 職員への外部キー t.integer :root_id # Messageへの外部キー t.integer :parent_id # Messageへの外部キー t.string :type, null: false # 継承カラム t.string :status, null: false, default: 'new' # 状態(職員向け) t.string :subject, null: false # 件名 t.text :body # 本文 t.text :remarks # 備考(職員向け) t.boolean :discarded, null: false, default: false # 顧客側の削除フラグ t.boolean :deleted, null: false, default: false # 職員側の削除フラグ t.timestamps end add_index :messages, [ :type, :customer_id ] add_index :messages, [ :customer_id, :discarded, :created_at ] add_index :messages, [ :type, :staff_member_id ] add_index :messages, [ :customer_id, :deleted, :created_at ] add_index :messages, [ :customer_id, :deleted, :status, :created_at ], name: 'index_messages_on_c_d_s_c' add_index :messages, [ :root_id, :deleted, :created_at ] add_foreign_key :messages, :customers add_foreign_key :messages, :staff_members add_foreign_key :messages, :messages, column: 'root_id' add_foreign_key :messages, :messages, column: 'parent_id' end end

マイグレーションの実行
$ rails db:migrate

エラー内容

== 20170827025146 CreateMessages: migrating =================================== -- create_table(:messages) -> 1.0368s -- add_index(:messages, [:type, :customer_id]) -> 0.6594s -- add_index(:messages, [:customer_id, :discarded, :created_at]) -> 0.6160s -- add_index(:messages, [:type, :staff_member_id]) -> 0.7579s -- add_index(:messages, [:customer_id, :deleted, :created_at]) -> 0.8821s -- add_index(:messages, [:customer_id, :deleted, :status, :created_at], {:name=> "index_messages_on_c_d_s_c"}) -> 0.6249s -- add_index(:messages, [:root_id, :deleted, :created_at]) -> 0.6584s -- add_foreign_key(:messages, :customers) -> 3.9734s -- add_foreign_key(:messages, :staff_members) -> 2.2416s -- add_foreign_key(:messages, :messages, {:column=>"root_id"}) rails aborted! StandardError: An error has occurred, all later migrations canceled: Column `root_id` on table `messages` has a type of `int(11)`. This does not match column `id` on `messages`, which has type `bigint(20)`. To resolve this issue, change the type of the `root_id` column on `messages` to be :integer. (For example `t.integer root_id`). Original message: Mysql2::Error: Cannot add foreign key constraint: ALTER TABLE `messages` ADD CONSTRAINT `fk_rails_a907b611e8` FOREIGN KEY (`root_id`) REFERENCES `messages` (`id`) /home/vagrant/chibi/chibi/db/migrate/20170827025146_create_messages.rb:28:in `ch ange' bin/rails:4:in `require' bin/rails:4:in `<main>' ActiveRecord::MismatchedForeignKey: Column `root_id` on table `messages` has a t ype of `int(11)`. This does not match column `id` on `messages`, which has type `bigint(20)`. To resolve this issue, change the type of the `root_id` column on `messages` to be :integer. (For example `t.integer root_id`). Original message: Mysql2::Error: Cannot add foreign key constraint: ALTER TABLE `messages` ADD CONSTRAINT `fk_rails_a907b611e8` FOREIGN KEY (`root_id`) REFERENCES `messages` (`id`) /home/vagrant/chibi/chibi/db/migrate/20170827025146_create_messages.rb:28:in `ch ange' bin/rails:4:in `require' bin/rails:4:in `<main>' Mysql2::Error: Cannot add foreign key constraint /home/vagrant/chibi/chibi/db/migrate/20170827025146_create_messages.rb:28:in `ch ange' bin/rails:4:in `require' bin/rails:4:in `<main>' Tasks: TOP => db:migrate (See full trace by running task with --trace)

とエラー表示されますがどこが悪さをしているか不明なのでどなたか解決方法を教えていただけないでしょうか?
宜しくお願いします。

追記:別のエラーが発生してしまいました。

== 20170827025146 CreateMessages: migrating =================================== -- create_table(:messages) rails aborted! StandardError: An error has occurred, all later migrations canceled: Index name 'index_messages_on_customer_id' on table 'messages' already exists/home/vagrant/chibi/chibi/db/migrate/20170827025146_create_messages.rb:3:in `change' bin/rails:4:in `require' bin/rails:4:in `<main>' ArgumentError: Index name 'index_messages_on_customer_id' on table 'messages' already exists /home/vagrant/chibi/chibi/db/migrate/20170827025146_create_messages.rb:3:in `change' bin/rails:4:in `require' bin/rails:4:in `<main>' Tasks: TOP => db:migrate (See full trace by running task with --trace)

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

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

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

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

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

guest

回答1

0

ベストアンサー

エラー内容を見た感じ、messageへの外部キーであるroot_idをint(11)で指定しているのが問題のようです。
root_idカラムをbigintで作成するように型を指定してやればうまく動くのではないでしょうか。

以下参考
http://qiita.com/Yinaura/items/cede8324d08993d2065c

投稿2017/08/27 04:07

wuzzy

総合スコア152

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

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

koume

2017/08/27 05:28

回答ありがとうございます。質問指せていただいたエラーは解消してくれたみたいですが、 $ rails db:migrateを実行したら別のエラーが発生してしまいました。 質問内容に追記させていただきましたのでみていただけないでしょうか?宜しくお願いします。
wuzzy

2017/08/27 05:45

index_messages_on_customer_idというindexはmessagesテーブルに既に存在する というエラーのようです。 一度実行した際に途中までmigrationのSQLが実行されているからかもしれません。
koume

2017/08/27 05:57

ありがとうございました。$rails db:migrate:resetで上手くいきました。 今後とも宜しくお願いします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問