LoginSignup
1
1

More than 3 years have passed since last update.

dependent: :destroyをつけていても削除できなかった原因

Posted at

長時間調べたが解決方法が見つからないという状態が続きました。
私のミスだったので、同じような状態の人に私の経験を伝えれればと思い書きます。

内容

紐付けしたオブジェクトを削除するために、dependent: :destroyをつけても、削除できない時に気をつけて欲しいことを書きます。

問題点

ある特定のUserのみ削除できない

モデルの関係性

UserCartが親子関係で、
CartProductCart_productsと 1対多 です。
Users ー Carts → Cart_products ← Products

class User < ApplicationRecord
  has_one :cart, dependent: :destroy
end

class Cart < ApplicationRecord
  belongs_to :user
  has_many :cart_products, dependent: :destroy
  has_many :products, through: :cart_products
end

class Product < ApplicationRecord
  has_many :cart_products, dependent: :destroy
end

class CartProduct < ApplicationRecord
  belongs_to :cart
  belongs_to :product
end

行っていたこと

関連付けられたオブジェクトもdestroy削除できるように
has_many,has_onedependent: :destroyをつけていた。

エラー文

Cannot delete or update a parent row: a foreign key constraint fails (`app_name_development`.`cart_products`, CONSTRAINT `fk_rails_a4f3e327f3` FOREIGN KEY (`cart_id`) REFERENCES `carts` (`id`))

原因

User has_one CartUser にはCartがは1つのみ紐づくようにしていたのですが、
削除できないUserには2つのCartが紐つかれていた。

そのため
User has_one CartUser has_many Cartsと変更すると削除できた。

+----+---------------------+---------------------+---------+
| id | created_at          | updated_at          | user_id |
+----+---------------------+---------------------+---------+
|  3 | 2020-02-21 07:07:57 | 2020-02-21 07:07:57 |       4 |
| 12 | 2020-02-21 07:29:37 | 2020-02-21 07:29:37 |       7 |
| 13 | 2020-02-21 07:34:26 | 2020-02-21 07:34:26 |       7 |
| 16 | 2020-02-22 07:51:47 | 2020-02-22 07:51:47 |      11 |
+----+---------------------+---------------------+---------+

今後の対処法

モデルを作成する時に、オプションとしてunique: trueを付け、userの重複をなしにする。

XXXXXXX_create_cart.rb
class CreateCarts < ActiveRecord::Migration[5.2]
  def change
    create_table :carts do |t|
      t.references :user, foreign_key: true, unique: true #ここ

      t.timestamps
    end
  end
end

結論

User has_one Cart 関係の
User に1つのみのCartが紐づくようにしていたのですが、
削除できないUserには2つのCartが紐つかれていた。

そのため、以下のようにUser has_one CartUser has_many Cartsと変更すると削除できた。

class User < ApplicationRecord
  has_one :cart, dependent: :destroy
end

⬇︎

class User < ApplicationRecord
  has_many :cart, dependent: :destroy
end
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1