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

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

新規登録して質問してみよう
ただいま回答率
85.51%
Ruby on Rails 6

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

Q&A

解決済

1回答

505閲覧

railsで開発中、うまくマイグレーションできず困っています

Tomojiri

総合スコア5

Ruby on Rails 6

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

0グッド

0クリップ

投稿2023/01/24 11:50

前提

Rails 6.0.5
mysql Ver 14.14
コードエディタ vsc
上記の環境で
あるテーブルにuser_idを追加する際にマイグレーションでエラーが発生しました。

実現したいこと

  • Incidentテーブルにuser_idを追加してマイグレーションを成功させたいです。

発生している問題・エラーメッセージ

rails db:migrate == 20230110082413 CreateIncidents: migrating ================================== -- create_table(:incidents) rails aborted! StandardError: An error has occurred, all later migrations canceled: Mysql2::Error: Table 'kuma_app_development.users' doesn't exist /Users/yukihito/projects/kuma-app/db/migrate/20230110082413_create_incidents.rb:3:in `change' /Users/yukihito/projects/kuma-app/bin/rails:9:in `<top (required)>' /Users/yukihito/projects/kuma-app/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: ActiveRecord::StatementInvalid: Mysql2::Error: Table 'kuma_app_development.users' doesn't exist /Users/yukihito/projects/kuma-app/db/migrate/20230110082413_create_incidents.rb:3:in `change' /Users/yukihito/projects/kuma-app/bin/rails:9:in `<top (required)>' /Users/yukihito/projects/kuma-app/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: Mysql2::Error: Table 'kuma_app_development.users' doesn't exist /Users/yukihito/projects/kuma-app/db/migrate/20230110082413_create_incidents.rb:3:in `change' /Users/yukihito/projects/kuma-app/bin/rails:9:in `<top (required)>' /Users/yukihito/projects/kuma-app/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/yukihito/projects/kuma-app/db/migrate/20230110082413_create_incidents.rb:3:in `change' /Users/yukihito/projects/kuma-app/bin/rails:9:in `<top (required)>' /Users/yukihito/projects/kuma-app/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)

該当のソースコード

20230110082413_create_incidents.rb

ruby

1class CreateIncidents < ActiveRecord::Migration[6.0] 2 def change 3 create_table :incidents do |t| 4 t.string :experience_years_id, null: false 5 t.string :type_id, null: false 6 t.string :case_level_id, null: false 7 t.string :management_level_id, null: false 8 t.string :place, null: false 9 t.datetime :date, null: false 10 t.string :patient_id, null: false 11 t.integer :age_id, null: false 12 t.integer :gender_id, null: false 13 t.text :case_content, null: false 14 t.text :opponent_factor, null: false 15 t.text :staff_factor, null: false 16 t.text :improvement_point, null: false 17 t.references :user, foreign_key: true 18 t.timestamps 19 end 20 end 21end

20230118081509_devise_create_users.rb

ruby

1class DeviseCreateUsers < ActiveRecord::Migration[6.0] 2 def change 3 create_table :users do |t| 4 ## Database authenticatable 5 t.string :email, null: false, default: "" 6 t.string :encrypted_password, null: false, default: "" 7 t.string :name, null: false 8 t.string :birthday, null: false 9 t.string :postal_code 10 t.string :address 11 t.integer :section_id, null: false 12 t.integer :job_id, null: false 13 14 ## Recoverable 15 t.string :reset_password_token 16 t.datetime :reset_password_sent_at 17 18 ## Rememberable 19 t.datetime :remember_created_at 20 21 ## Trackable 22 # t.integer :sign_in_count, default: 0, null: false 23 # t.datetime :current_sign_in_at 24 # t.datetime :last_sign_in_at 25 # t.string :current_sign_in_ip 26 # t.string :last_sign_in_ip 27 28 ## Confirmable 29 # t.string :confirmation_token 30 # t.datetime :confirmed_at 31 # t.datetime :confirmation_sent_at 32 # t.string :unconfirmed_email # Only if using reconfirmable 33 34 ## Lockable 35 # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts 36 # t.string :unlock_token # Only if unlock strategy is :email or :both 37 # t.datetime :locked_at 38 39 40 t.timestamps null: false 41 end 42 43 add_index :users, :email, unique: true 44 add_index :users, :reset_password_token, unique: true 45 # add_index :users, :confirmation_token, unique: true 46 # add_index :users, :unlock_token, unique: true 47 end 48end

models/incident.rb

ruby

1class Incident < ApplicationRecord 2 belongs_to :user 3end

models/user.rb

ruby

1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 7 extend ActiveHash::Associations::ActiveRecordExtensions 8 belongs_to_active_hash :section 9 belongs_to_active_hash :job 10 has_many :incidents 11 12 validates :name, presence: true 13 validates :birthday, presence: true 14 VALID_POSTAL_CODE_REGEX = /\A\d{3}[-]?\d{4}\z/ 15 validates :postal_code, presence: true, format: { with: VALID_POSTAL_CODE_REGEX } 16 validates :section_id, numericality: { other_than: 1, message: "can't be blank" } 17 validates :job_id, numericality: { other_than: 1, message: "can't be blank" } 18end
  • 上記のファイルが怪しいと思っています。

試したこと

  • モデル間のアソシエーションを見直しました。
  • マイグレーションファイルのincidentsの t.references :user, foreign_key: trueの部分で foreign_key: trueを削除するとマイグレーションは成功しましたが、user_idが自動入力できない状態で意図した動作ではなくなってしまいました。
  • 文字の間違いも疑い、見直しています。

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

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

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

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

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

yuma.inaura

2023/01/24 12:09

usersテーブルを作る前に他テーブルを作成していませんか?順序的に
guest

回答1

0

自己解決

回答ありがとうございます!
マイグレーションに順番が関係あるということですね!
順番が違うことでテーブルの内容が無いため、エラーになっているという事ですね。
単純なことでした、ありがとうございます😊

投稿2023/01/24 12:22

Tomojiri

総合スコア5

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.51%

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

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

質問する

関連した質問