簡體   English   中英

Admin :: UsersController#destroy中的ActiveRecord :: InvalidForeignKey

[英]ActiveRecord::InvalidForeignKey in Admin::UsersController#destroy

在ActiveAdmin中,當我要刪除用戶時,出現以下錯誤:

ActiveRecord::InvalidForeignKey in Admin::UsersController#destroy 
PG::ForeignKeyViolation: ERROR: update or delete on table "users" violates foreign key constraint "fk_rails_b080fb4855" on table "notifications" DETAIL: Key (id)=(15) is still referenced from table "notifications". : DELETE FROM "users" WHERE "users"."id" = $1

我的用戶模型中已經有了has_many :notifications, dependent: :destroy ,所以我不理解該錯誤。

通知模型:

class Notification < ActiveRecord::Base
  before_validation :check_modeles

  validates :message, presence: true
  validates :modele, presence: true
  validates :user_id, presence: true
  validates :contact_id, presence: true
  validates_associated :user

  belongs_to :user
  belongs_to :contact, :class_name => "User", :foreign_key => "contact_id" 

用戶模型:

class User < ActiveRecord::Base
  before_validation :check_genres
  before_validation :set_image
  before_validation :init_attrs
  before_create :set_name
  before_create :create_mangopay
  after_create :set_centres_interets
  after_create :set_preferences_musicales
  after_create :check_on_create
  after_update :check_on_update
  before_update :create_mangopay_bank_account
  before_destroy :remove_contact_notifications




  acts_as_mappable :default_units => :kms,
                   :lat_column_name => :last_lat,
                   :lng_column_name => :last_lng


  devise :database_authenticatable, :registerable,
    :recoverable, :rememberable, :trackable, :validatable, :omniauthable

  has_one :fil, dependent: :destroy
  has_one :preferences_voyage,  dependent: :destroy
  has_one :verification,  dependent: :destroy
  has_many :badges,  dependent: :destroy
  has_many :favoris ,  dependent: :destroy
  has_many :centres_interets,  dependent: :destroy
  has_many :preferences_musicales,  dependent: :destroy
  has_many :recommandations,  dependent: :destroy
  has_many :reputations,  dependent: :destroy
  has_many :reservations,  dependent: :destroy
  has_many :routes,  dependent: :destroy
  has_many :trajets_reguliers,  dependent: :destroy
  has_many :vehicules,  dependent: :destroy
  has_many :contact_notifications, foreign_key: 'contact_id', class_name: 'Notification', dependent: :destroy
  has_many :notifications,  dependent: :destroy
  has_many :recherches, dependent: :destroy
  has_many :blocages, dependent: :destroy
  has_many :messageries, dependent: :destroy
  has_many :messages, dependent: :destroy

  validates :date_naissance, presence: true
  validates :first_name, presence: true
  validates :last_name, presence: true

也許has_many :notifications, dependent: :destroy僅影響用戶而不影響聯系? 如果是這樣,我該如何解決?

好的,也許您最好的選擇就是刪除外鍵約束。

使用以下行創建遷移

remove_foreign_key :notifications, :users
remove_foreign_key :notifications, column: :contact_id

Rails不需要外鍵約束,並且顯然在這里沒有幫助。

您也可以按名稱刪除它...

remove_foreign_key :notifications, name: :fk_rails_b080fb4855

在PostgreSQL數據庫表notifications您有兩個與users user_idcontact_id以及dependent: destroy users有關的不同外鍵dependent: destroy僅與user_id

向您的用戶模型添加另一個has_many以覆蓋第二個關聯

has_many :contact_notifications, foreign_key: 'contact_id', class_name: 'Notification', dependent: :destroy

另外,請確保在刪除用戶時使用的是destroy方法

@user.delete # will NOT run callbacks
@user.destroy # will run callbacks including destroying dependent records

為確保問題與contact_id有關,您可以嘗試通過手動過程刪除這些記錄

class User

  before_destroy :remove_contact_notifications

  def remove_contact_notifications
    Notification.where(contact_id: id).destroy_all
    Notification.where(user_id: id).destroy_all
  end

end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM