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

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

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

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

Q&A

1回答

307閲覧

オリジナルアプリ作成中です。テーブルに外部キーが追加できなくて困っています。

Fumi_Kazu5

総合スコア1

Ruby on Rails

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

0グッド

0クリップ

投稿2021/02/10 07:14

編集2021/02/10 12:08

ターミナルにてテーブルに外部キーを作成し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

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

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

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

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

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

guest

回答1

0

多分、20210129112855_create_shoppings.rb の前に table owners が作られて居ないのでしょう。
rails db:migrate:status の結果と
20210129112855_create_shoppings.rb とを
載せてください。

投稿2021/02/10 10:45

winterboum

総合スコア23329

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

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

winterboum

2021/02/10 11:14 編集

画像でなくtextで載せて コメントではなく質問欄を編集して
Fumi_Kazu5

2021/02/10 11:54

すみません。投稿内容を訂正します。
winterboum

2021/02/10 11:59

20210129112855_create_shoppings.rb は?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問