2016-09-16 70 views
0

在ActiveAdmin,當我要刪除我有以下的錯誤用戶:的ActiveRecord :: InvalidForeignKey在管理:: UsersController#破壞

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隻影響用戶,而不是接觸?如果是這樣,我該如何解決它?

回答

0

在你的PostgreSQL數據庫表notifications你有涉及到usersuser_idcontact_iddependent: 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 
+0

我仍然有同樣的錯誤:/ –

+0

我已經添加了另一點到我的答案,讓我知道是否有幫助! – SteveTurczyn

+0

顯然,在ActiveAdmin中使用的方法是「刪除」,但我已經測試刪除用戶與依賴記錄,它刪除用戶與記錄,所以我不知道...在User模型中返回代碼返回同樣的錯誤,我很迷茫... –

1

好吧,也許你最好的選擇就是去掉外鍵限制。

下列行

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

Rails的創建遷移不需要外鍵約束,而且他們顯然不是在這裏幫助。

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

remove_foreign_key :notifications, name: :fk_rails_b080fb4855 
+0

我將與主開發人員一起看到刪除密鑰,因爲我不想破壞應用程序哈哈 –