I'm trying to do a simple user.destroy
but running into the following error:
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".
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
I'm new to foreign keys and references, so I'm not sure at all how to fix this.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…