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

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

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

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

Q&A

解決済

1回答

958閲覧

rails db:migrateにてエラーが出る

kenta34344

総合スコア5

Ruby on Rails

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

0グッド

0クリップ

投稿2021/08/25 11:33

前提・実現したいこと

フォームオブジェクトによりstock_itemsテーブルとtagsテーブルに登録出来るようにしたい。その為に中間テーブルを作成したいがエラーが出てしまう。

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

StandardError:

1 2Column `stock_item_id` on table `item_connects` does not match column `id` on `stock_items`, which has type `bigint(20)`. To resolve this issue, change the type of the `stock_item_id` column on `item_connects` to be :bigint. (For example `t.bigint :stock_item_id`). 3Original message: Mysql2::Error: Cannot add foreign key constraint 4/Users/watanabekenta/orijinals/n-web_app/db/migrate/20210824112715_create_item_connects.rb:3:in `change' 5/Users/watanabekenta/orijinals/n-web_app/bin/rails:9:in `<top (required)>' 6/Users/watanabekenta/orijinals/n-web_app/bin/spring:15:in `<top (required)>' 7bin/rails:3:in `load' 8bin/rails:3:in `<main>' 9 10Caused by: 11ActiveRecord::MismatchedForeignKey: Column `stock_item_id` on table `item_connects` does not match column `id` on `stock_items`, which has type `bigint(20)`. To resolve this issue, change the type of the `stock_item_id` column on `item_connects` to be :bigint. (For example `t.bigint :stock_item_id`). 12Original message: Mysql2::Error: Cannot add foreign key constraint 13/Users/watanabekenta/orijinals/n-web_app/db/migrate/20210824112715_create_item_connects.rb:3:in `change' 14/Users/watanabekenta/orijinals/n-web_app/bin/rails:9:in `<top (required)>' 15/Users/watanabekenta/orijinals/n-web_app/bin/spring:15:in `<top (required)>' 16bin/rails:3:in `load' 17bin/rails:3:in `<main>' 18 19Caused by: 20Mysql2::Error: Cannot add foreign key constraint 21/Users/watanabekenta/orijinals/n-web_app/db/migrate/20210824112715_create_item_connects.rb:3:in `change' 22/Users/watanabekenta/orijinals/n-web_app/bin/rails:9:in `<top (required)>' 23/Users/watanabekenta/orijinals/n-web_app/bin/spring:15:in `<top (required)>' 24bin/rails:3:in `load' 25bin/rails:3:in `<main>' 26Tasks: TOP => db:migrate 27(See full trace by running task with --trace) 28エラーメッセージ

該当のソースコード

class CreateItemConnects < ActiveRecord::Migration[6.0] def change create_table :item_connects do |t| t.references :stock_item, foreign_key: true t.references :tag_word, foreign_key: true t.timestamps end end end

tagrb

1class Tag < ActiveHash::Base 2 self.data = [ 3 { id: 1, name: '--' }, 4 { id: 2, name: '和食' }, 5 { id: 3, name: '洋食' }, 6 { id: 4, name: '中華' }, 7 { id: 5, name: '揚げ' }, 8 { id: 6, name: '炒め' }, 9 { id: 7, name: '湯せん' }, 10 { id: 8, name: '焼き' }, 11 { id: 9, name: '蒸し' }, 12 { id: 10, name: '自然解凍' }, 13 { id: 11, name: '鍋' }, 14 { id: 12, name: '牛' }, 15 { id: 13, name: '豚' }, 16 { id: 14, name: '鶏' }, 17 { id: 15, name: '海鮮' }, 18 { id: 16, name: '山菜' }, 19 { id: 17, name: '野菜' }, 20 { id: 18, name: 'ラーメン' }, 21 { id: 19, name: 'うどん' }, 22 { id: 20, name: 'そば' }, 23 { id: 21, name: 'パスタ' }, 24 { id: 22, name: 'パン' }, 25 { id: 23, name: 'ご飯' }, 26 { id: 24, name: 'パン' }, 27 { id: 25, name: '丼' }, 28 { id: 26, name: 'オードブル' }, 29 { id: 27, name: '弁当' }, 30 { id: 28, name: '副菜' }, 31 { id: 29, name: '先付' }, 32 { id: 30, name: '椀種' }, 33 { id: 31, name: '八寸' }, 34 { id: 32, name: 'お子様' }, 35 { id: 33, name: 'スイーツ' }, 36 { id: 34, name: '洋菓子' }, 37 { id: 35, name: '和菓子' }, 38 { id: 36, name: '調味料' }, 39 { id: 37, name: 'ドレッシング' }, 40 { id: 38, name: 'タレ' }, 41 { id: 39, name: '乾物' }, 42 ] 43 44 include ActiveHash::Associations 45 has_many :item_connects 46 has_many :stock_items, through: :item_connects 47 48end

stockitem

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

itemconnect

1class ItemConnect < ApplicationRecord 2 3 belongs_to :stock_item 4 5 extend ActiveHash::Associations:ActiveRecord ActiveRecordExtensions 6 belongs_to :tag 7end

試したこと

何かしらの原因によりstock_itemテーブルの情報が参照できないのでしょうか?
rails db:migrate:resetしても変わらず
アソシエーション漏れもなさそうです
どなたか力を貸して下さい。

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

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

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

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

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

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

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

guest

回答1

0

自己解決

テーブル作成時にforeign_key: { to_table: :stock_items }と参照テーブルを付け加えることで可能になりました。

投稿2021/08/26 11:36

kenta34344

総合スコア5

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問