尝试做一个简单的user.destroy但遇到以下错误:
错误:表"user"上的更新或删除违反了表"identities"上的外键约束"fk_rails_5373344100"DETAIL: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
Run Code Online (Sandbox Code Playgroud)
这是我的用户和身份模型:
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
Run Code Online (Sandbox Code Playgroud)
和用户:
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, …Run Code Online (Sandbox Code Playgroud)