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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

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

Ruby on Rails

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

Q&A

1回答

1849閲覧

外部キー制約のついたレコードを削除しようとしてエラー

ray4322

総合スコア1

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/03/21 16:34

前提・実現したいこと

Ruby on Railsでオリジナルアプリを作成中の初学者です。

twitterのようなアプリで既にいいねしてある投稿を削除しようとしたらエラーが発生してそこから抜けられていません。

#開発環境

docker
Rails 6.1.1
ruby ruby 3.0.0
MySQL 8.0

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

既にいいねしてある投稿を削除しようとしたら

ActiveRecord::InvalidForeignKey in PostsController#destroy Mysql2::Error: Cannot delete or update a parent row: a foreign key constraint fails (`workout_app_development`.`favorites`, CONSTRAINT `fk_rails_c3d75da8fd` FOREIGN KEY (`favorite_id`) REFERENCES `posts` (`id`))

というエラーが発生したので

post.rbの :favoriteに下記のように dependent: :destroyを加えました。

class Post < ApplicationRecord belongs_to :user has_many :favorites, dependent: :destroy has_many :comments has_many :tag_relationships, dependent: :destroy has_many :tags, through: :tag_relationships validates :content, presence: true, length: { maximum: 255 } validates :title, presence: true, length: { maximum: 30 } end

その後ブラウザを更新してみると下記のエラーになりました。

ActiveRecord::StatementInvalid in PostsController#destroy Mysql2::Error: Unknown column 'favorites.post_id' in 'where clause'

これは「無効な記述です。favorites.post_idカラムはありません。」というようなことを言われているのでしょうか?

favoritesテーブルにはuser_idとfavorite_idというカラムをしており、post_idカラムは作成しておりません。

schema.rb

create_table "comments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "content" t.bigint "user_id", null: false t.bigint "post_id", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["post_id"], name: "index_comments_on_post_id" t.index ["user_id"], name: "index_comments_on_user_id" end create_table "favorites", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.bigint "user_id", null: false t.bigint "favorite_id", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["favorite_id"], name: "index_favorites_on_favorite_id" t.index ["user_id", "favorite_id"], name: "index_favorites_on_user_id_and_favorite_id", unique: true t.index ["user_id"], name: "index_favorites_on_user_id" end create_table "posts", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "content" t.bigint "user_id", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.string "title" t.string "youtube_url" t.text "note" t.string "target_muscle" t.index ["user_id"], name: "index_posts_on_user_id" end create_table "relationships", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.bigint "user_id", null: false t.bigint "follow_id", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["follow_id"], name: "index_relationships_on_follow_id" t.index ["user_id", "follow_id"], name: "index_relationships_on_user_id_and_follow_id", unique: true t.index ["user_id"], name: "index_relationships_on_user_id" end create_table "tag_relationships", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.bigint "post_id", null: false t.bigint "tag_id", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["post_id", "tag_id"], name: "index_tag_relationships_on_post_id_and_tag_id", unique: true t.index ["post_id"], name: "index_tag_relationships_on_post_id" t.index ["tag_id"], name: "index_tag_relationships_on_tag_id" end create_table "tags", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "tag_name" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end create_table "users", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "name" t.string "email" t.string "password_digest" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end add_foreign_key "comments", "posts" add_foreign_key "comments", "users" add_foreign_key "favorites", "posts", column: "favorite_id" add_foreign_key "favorites", "users" add_foreign_key "posts", "users" add_foreign_key "relationships", "users" add_foreign_key "relationships", "users", column: "follow_id" add_foreign_key "tag_relationships", "posts" add_foreign_key "tag_relationships", "tags" end

favoritesテーブル作成時のマイグレーションファイル

class CreateFavorites < ActiveRecord::Migration[6.1] def change create_table :favorites do |t| t.references :user, null: false, foreign_key: true t.references :favorite, null: false, foreign_key: { to_table: :posts } t.timestamps t.index [:user_id, :favorite_id], unique: true end end end

マイグレーションファイルの{ to_table: :posts }という指定の仕方に問題があるのでしょうか?

どなたかご教授お願いいたします。

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

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

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

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

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

winterboum

2021/08/23 00:29

この model Postの定義でこのエラーになるのはとても興味深い。 おそらく model Favorite の関連定義が怪しげなのでしょう。 それ載せてください。 で、そのとき Favoriteは誰が何にお気に入りするのか、を日本語で一言書いておいてください。多分多くの人の想像通りだろうとは思うのですが、「このエラーは何だ?」 と言う点があるので二度手間にならないよう念の為。
guest

回答1

0

postsテーブルとfavoritesテーブルを紐づけるために
post.rbにhas_many :favorites

としていますがこの場合
favorite.rbにも
belongs_to :post
が必要になります。
ですがこの場合railsのルールとして
favoritesテーブルにpost_idカラムを追加する必要があるのです

投稿2021/08/22 23:55

HTMLdoc

総合スコア67

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問