尝试做一个简单的user.destroy但运行到以下错误:
错误:表“用户”上的更新或删除违反表“身份”上的外键约束“fk_rails_5373344100”
详细信息:Key(id)=(2)仍然从表“identity”引用.
这是我的身份迁移
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
这是我的用户和身份模型:
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
和用户:
class User < ActiveRecord::Base TEMP_EMAIL_PREFIX = 'ricky@writeit.com' TEMP_EMAIL_REGEX = /\ricky@writeit.com/ # 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
我新的外键和引用,所以我不知道如何解决这个问题.任何帮助将不胜感激.
谢谢