ターミナルにてテーブルに外部キーを作成しrails db:migrateすると以下のエラーが出てしまいます。原因が分からず途方に暮れています。
ご教授頂けると助かります。
rails aborted!
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: Table 'tomori_app_development.owners' doesn't exist
/Users/fumi/projects/tomori_app/db/migrate/20210129112855_create_shoppings.rb:3:in change' /Users/fumi/projects/tomori_app/bin/rails:9:in
<top (required)>'
/Users/fumi/projects/tomori_app/bin/spring:15:in <top (required)>' bin/rails:3:in
load'
bin/rails:3:in `<main>'
Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'tomori_app_development.owners' doesn't exist
/Users/fumi/projects/tomori_app/db/migrate/20210129112855_create_shoppings.rb:3:in change' /Users/fumi/projects/tomori_app/bin/rails:9:in
<top (required)>'
/Users/fumi/projects/tomori_app/bin/spring:15:in <top (required)>' bin/rails:3:in
load'
bin/rails:3:in `<main>'
Caused by:
Mysql2::Error: Table 'tomori_app_development.owners' doesn't exist
/Users/fumi/projects/tomori_app/db/migrate/20210129112855_create_shoppings.rb:3:in change' /Users/fumi/projects/tomori_app/bin/rails:9:in
<top (required)>'
/Users/fumi/projects/tomori_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/fumi/projects/tomori_app/db/migrate/20210129112855_create_shoppings.rb:3:in change' /Users/fumi/projects/tomori_app/bin/rails:9:in
<top (required)>'
/Users/fumi/projects/tomori_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)
説明が足りなくてすみません。追加記入です。
rails db:migrate:statusの結果
database: tomori_app_development
Status Migration ID Migration Name
up 20210126060600 Devise create users
up 20210126062512 Create active storage tablesactive storage
up 20210129112855 Create shoppings
up 20210131072013 Create orders
up 20210131143313 Devise create owners
モデルの記入内容
class Shopping < ApplicationRecord
belongs_to :owner, optional: true, foreign_key: :owner_id
has_one :order
has_one_attached :image
extend ActiveHash::Associations::ActiveRecordExtensions
with_options presence: true do
validates :product_name
validates :price
validates :image
end
end
データベース
class CreateShoppings < ActiveRecord::Migration[6.0]
def change
create_table :shoppings do |t|
t.string :product_name, null: false
t.integer :price, null: false
#t.references :owner, foreign_key: true
t.timestamps end
end
end
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/02/10 11:02
2021/02/10 11:14 編集
2021/02/10 11:54
2021/02/10 11:59