0
0

More than 1 year has passed since last update.

外部キー制約ついているデータの削除

Posted at

外部キー制約がついているデータの削除について

外部キー制約がついているデータを削除するために必要なことを簡単にまとめました。
外部キー制約がなければ、destroyアクションで削除機能を実装する事ができますが、外部キー制約があると削除を実行するとエラーが出てしまします。

エラーが出た時のコード

app> models> user.rb
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  with_options presence: true do
    validates :name
    validates :profile
    validates :occupation
    validates :position
  end

  has_many :prototypes
  has_many :comments
end
app> models> prototype.rb
class Prototype < ApplicationRecord
  with_options presence: true do
    validates :title
    validates :catch_copy
    validates :concept
    validates :image
  end

  belongs_to :user
  has_one_attached :image
  has_many :comments
end

修正した手順

テーブル設計を確認する

README
## usersテーブル

| Column             | Type   | Options                   |
| ------------------ | ------ | ------------------------- |
| email              | string | null: false, unique: true |
| encrypted_password | string | null: false               |
| name               | string | null: false               |
| profile            | text   | null: false               |
| occupation         | text   | null: false               |
| position           | text   | null: false               |

### Association
- has_many :prototypes
- has_many :comments

## prototypesテーブル

| Column      | Type       | Options                        |
| ----------- | ---------- | ------------------------------ |
| title       | string     | null: false                    |
| catch_copy  | text       | null: false                    |
| concept     | text       | null: false                    |
| user        | references | null: false, foreign_key: true |  ←外部キー制約

### Association
- belongs_to :user
- has_many :comments

## commentsテーブル

| Column     | Type       | Options                        |
| ---------- | ---------- | ------------------------------ |
| content    | text       | null: false                    |
| prototype  | references | null: false, foreign_key: true |  ←外部キー制約
| user       | references | null: false, foreign_key: true |  ←外部キー制約

### Association
- belongs_to :user
- belongs_to :prototype

以上から、3箇所の外部キー制約が確認できます。
次の記述を追記すると削除が可能になります。

該当するカラム名に設定する
dependent: :destroy

各モデルの修正

app> models> user.rb
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  with_options presence: true do
    validates :name
    validates :profile
    validates :occupation
    validates :position
  end

  has_many :prototypes, dependent: :destroy
  has_many :comments, dependent: :destroy
end
app> models> prototype.rb
class Prototype < ApplicationRecord
  with_options presence: true do
    validates :title
    validates :catch_copy
    validates :concept
    validates :image
  end

  belongs_to :user
  has_one_attached :image
  has_many :comments, dependent: :destroy
end

修正した結果

修正前に出ていたエラーが解消されて、削除したいデータが削除されました。
何故その記述が必要なのか、何処を修正するのかは検索しながらの手探り状態ではありますが、プログラムなのでルールさえわかれば少しずつでも理解していけます。
さらに深掘りたい方は、詳細に記載されている方のブログを探してみてください。

0
0
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
0
0