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

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

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

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

Q&A

解決済

1回答

707閲覧

外部キーを紐付けようとするとエラーが出る。

kenta34344

総合スコア5

Ruby on Rails

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

0グッド

0クリップ

投稿2021/09/28 11:55

前提・実現したいこと

stock_itemsテーブルとtagsテーブル(多対多)をデータベースにreference型で外部キーを設定したい

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

    down 20210804105711 Create stock items down 20210804111228 Create tags down 20210824112715 Create item connects watanabekenta@GV9395 n-web_app % rails db:migrate == 20210804105711 CreateStockItems: migrating ================================= -- create_table(:stock_items) rails aborted! StandardError: An error has occurred, all later migrations canceled: Mysql2::Error: Table 'n_web_app_development.tags' doesn't exist /Users/watanabekenta/orijinals/n-web_app/db/migrate/20210804105711_create_stock_items.rb:3:in `change' /Users/watanabekenta/orijinals/n-web_app/bin/rails:9:in `<top (required)>' /Users/watanabekenta/orijinals/n-web_app/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' エラーメッセージ

該当のソースコード

stockitem

1class CreateStockItems < ActiveRecord::Migration[6.0] 2 def change 3 create_table :stock_items do |t| 4 t.string :stock_item_manufacturer, null: false 5 t.string :stock_item_name, null: false 6 t.string :stock_item_standard, null: false 7 t.string :stock_item_strage_condition, null: false 8 t.text :stock_item_description, null: false 9 t.references :tag, foreign_key: true 10 t.timestamps 11 end 12 end 13end

tag

1class CreateTags < ActiveRecord::Migration[6.0] 2 def change 3 create_table :tags do |t| 4 t.string :tag_word, null: false, uniqueness: true 5 t.references :stock_item, foreign_key: true 6 t.timestamps 7 end 8 end 9end

stockitemrb

1class StockItem < ApplicationRecord 2 3 has_one_attached :image 4 has_many :item_connects, dependent: :destroy 5 has_many :tags, through: :item_connects 6 7 extend ActiveHash::Associations::ActiveRecordExtensions 8 belongs_to :genre 9 10end 11

tagrb

1class Tag < ApplicationRecord 2 3 has_many :item_connects 4 has_many :stock_items, through: :item_connects 5 6end

itemconnectrb

1class ItemConnect < ApplicationRecord 2 3 belongs_to :stock_item 4 belongs_to :tag 5end 6

stockitemcontroller

1class StockItemsController < ApplicationController 2 3 before_action :search_stock_item, only: [:index, :search] 4 5 def index 6 @stock_items = StockItem.all.order(created_at: :desc) 7 end 8 9 def new 10 @stock_item = ItemsTag.new 11 end 12 13 def create 14 @stock_item = ItemsTag.new(stock_item_params) 15 if @stock_item.save 16 return redirect_to root_path 17 else 18 render "new" 19 end 20 end 21 22 def edit 23 @stock_item = StockItem.find(params[:id]) 24 end 25 26 def update 27 if @stock_item.update(stock_item_params) 28 redirect_to contacts_path 29 else 30 render :edit 31 end 32 end 33 34 def destroy 35 @stock_item = StockItem.find(params[:id]) 36 if @stock_item.destroy 37 redirect_to root_path 38 else 39 render :index 40 end 41 end 42 43 def search 44 @results = @p.result.includes(:tag) 45 end 46 47 private 48 49 def stock_item_params 50 params.require(:items_tag).permit(:stock_item_manufacturer, :stock_item_name, :stock_item_standard, :stock_item_strage_condition, :stock_item_description, :image, :tag_word) 51 end 52 53 def search_stock_item 54 @p = StockItem.ransack(params[:q]) 55 end 56 57end

試したこと

rails db:migrate:reset
アソシエーションの確認しましたがおかしな部分が見つかりません。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

ベストアンサー

migrationファイルのフィル名が省略されているのでエラーメッセージから見た予想ですが、
CreateStockItems より先に CreateItems が行われていないために 'n_web_app_development.tags' doesn't exist というエラーになっています。
ですが、、、、
この状態で順番を逆にすると今度は 'n_web_app_development.stock_tags' doesn't exist になりますね。
互いに外部keyにし合うというのが原因です。

投稿2021/09/28 12:34

winterboum

総合スコア23416

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

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

kenta34344

2021/09/28 13:37

up 20210619232522 Devise create users up 20210623120129 Create active storage tablesactive storage up 20210623120504 Create items up 20210629112109 Create school lunches up 20210706100909 ********** NO FILE ********** up 20210707113625 Create contacts up 20210710102321 Create user contacts down 20210804105711 Create stock items down 20210804111228 Create tags down 20210824112715 Create item connects 回答ありがとうございます。 Itemsテーブルが関係してきますか? 互いにとの事でしたので、tagsテーブル側の外部キーを外しても変化なかったです。
winterboum

2021/09/28 22:42

ですから、順番です。 t.references :tag, foreign_key: true の前に Create tags がひつよう
kenta34344

2021/10/01 12:18

順番を変える方法を調べましたが出てきませんでした。一度削除するしかないでしょうか。そうすると今度はCreate item connectsとの順番が逆になってしまいます。
winterboum

2021/10/08 12:20

file名の前についている 年月日時分秒を編集して順番を変えてください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問