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

时间:2016-09-16 11:07:45

标签: ruby-on-rails ruby-on-rails-3 activeadmin

在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只影响用户而不影响联系人?如果是这样,我该如何解决?

2 个答案:

答案 0 :(得分:2)

好吧,也许你最好的办法就是删除外键约束。

使用以下行创建迁移

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

Rails不需要外键约束,它们显然没有帮助。

您也可以按名称将其删除......

remove_foreign_key :notifications, name: :fk_rails_b080fb4855

答案 1 :(得分:0)

在PostgreSQL数据库表notifications中,您有两个与users user_idcontact_id以及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