Ruby-on-rails 由于外键约束无法删除对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29544693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:11:01  来源:igfitidea点击:

can't delete object due to foreign key constraint

ruby-on-railspostgresql

提问by Ricky Mason

Trying to do a simple user.destroybut running into the following error:

试图做一个简单user.destroy但遇到以下错误:

ERROR: update or delete on table "users" violates foreign key constraint "fk_rails_5373344100" on table "identities" DETAIL: Key (id)=(2) is still referenced from table "identities".

错误:更新或删除表“用户”违反了表“身份”上的外键约束“fk_rails_5373344100”详细信息:键(id)=(2)仍然从表“身份”中引用。

Here is my migration for Identities

这是我的身份迁移

class CreateIdentities < ActiveRecord::Migration
  def change
    create_table :identities do |t|
      t.references :user, index: true, foreign_key: true
      t.string :provider
      t.string :uid

      t.timestamps null: false
    end
  end
end

Here is my user and identity model:

这是我的用户和身份模型:

class Identity < ActiveRecord::Base
  belongs_to :user

  validates_presence_of :uid, :provider
  validates_uniqueness_of :uid, :scope => :provider

  def self.find_for_oauth(auth)
    find_or_create_by(uid: auth.uid, provider: auth.provider)
  end
end

and user:

和用户:

class User < ActiveRecord::Base
  TEMP_EMAIL_PREFIX = '[email protected]'
  TEMP_EMAIL_REGEX = /\[email protected]/

  # Include default devise modules. Others available are:
  # :lockable, :timeoutable
  devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable

  validates_format_of :email, :without => TEMP_EMAIL_REGEX, on: :update
...

end

Im new to foreign keys and references, so I'm not sure at all how to fix this. Any help would be greatly appreciated.

我是外键和引用的新手,所以我完全不知道如何解决这个问题。任何帮助将不胜感激。

Thanks

谢谢

回答by Doon

You would need to remove the Identity that references the user first. Then you can delete the user.. By default the foreign key is doing a restrictso you cannot delete the user if anything references to it.

您需要先删除引用用户的身份。然后您可以删除该用户。默认情况下,外键正在执行 a,restrict因此如果有任何内容引用该用户,您将无法删除该用户。

if you would like use Rails to handle destroying the identity you can do

如果你想使用 Rails 来处理破坏你可以做的身份

class User < ActiveRecord::Base
  has_many :identities,  dependent: :destroy 

  ......

 end 

Which would cause Rails to destroy all the dependent records.

这会导致 Rails 销毁所有相关记录。

But as you are using Foreign keys, you can adjust your migration to set cascade deletes

但是当您使用外键时,您可以调整迁移以设置级联删除

 add_foreign_key :identities, :users, on_delete: :cascade

Assuming rails 4.2 which has native support

假设 Rails 4.2 具有本机支持

回答by Paul Richter

An easy solution is to simply cascade-delete the records in the associated table, which can be done through active record, like so:

一个简单的解决方案是简单地级联删除关联表中的记录,这可以通过活动记录完成,如下所示:

user.rb

用户名

class User < ActiveRecord::Base
  has_many :identities, dependent: :destroy

  # rest of user class
end

Check out the documentation pertaining to has_manyfor more info.

查看与 相关has_many文档以获取更多信息。