gpt4 book ai didi

ruby-on-rails - Admin::UsersController 中的 ActiveRecord::InvalidForeignKey#destroy

转载 作者:行者123 更新时间:2023-12-04 03:19:02 32 4
gpt4 key购买 nike

在 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

关于ruby-on-rails - Admin::UsersController 中的 ActiveRecord::InvalidForeignKey#destroy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39529998/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com