我试图做一个简单的user.destroy
,但遇到了以下错误:
错误:表"用户"上的更新或删除违反了表"标识"上的外键约束"fk_rails_5373344100"
这是我的身份迁移
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
我不熟悉外键和引用,所以我根本不知道如何修复它.