Commit 5d99c3d2 authored by David Lucadou's avatar David Lucadou

Fixed error where users could not delete their accounts

Basically, I moved dependent nullification from the User model to the
Message migration file, as I cannot figure out how to target multiple
foreign keys in a Model.
parent 43cc0ac1
Pipeline #62504126 canceled with stage
in 6 seconds
......@@ -15,7 +15,15 @@ class User < ApplicationRecord
has_many :bans, dependent: :nullify
has_many :credentials, dependent: :destroy # FIDO2 U2F WebAuthn tokens
has_many :conversations, dependent: :nullify
has_many :messages, dependent: :nullify
has_many :messages
# No dependent: :nullify here because each message can have 2 users -
# the sender and the deleter. I cannot figure out how to tell it to
# nullify both, instead it tries to nullify user_id, which does not
# exist (the keys are sender_id and deleter_id).
# Instead, I set it to nullify the dependents in the migration file
# which I used to define the Messages table, and it works fine.
# Trying to use dependent: :nullify here causes errors when users
# attempt to delete their accounts.
has_many :reports, dependent: :nullify
has_many :searchs, dependent: :nullify
# dependent: :nullify means deleting the (parent) user doesn't force
......
......@@ -19,7 +19,7 @@ class CreateMessages < ActiveRecord::Migration[5.2]
t.timestamps
end
add_foreign_key :messages, :users, column: :sender_id
add_foreign_key :messages, :users, column: :deleter_id
add_foreign_key :messages, :users, column: :sender_id, on_delete: :nullify
add_foreign_key :messages, :users, column: :deleter_id, on_delete: :nullify
end
end
......@@ -237,8 +237,8 @@ ActiveRecord::Schema.define(version: 2019_05_18_165018) do
add_foreign_key "conversations", "users"
add_foreign_key "credentials", "users"
add_foreign_key "messages", "conversations"
add_foreign_key "messages", "users", column: "deleter_id"
add_foreign_key "messages", "users", column: "sender_id"
add_foreign_key "messages", "users", column: "deleter_id", on_delete: :nullify
add_foreign_key "messages", "users", column: "sender_id", on_delete: :nullify
add_foreign_key "reports", "users"
add_foreign_key "searches", "users"
add_foreign_key "user_preferences", "users"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment