0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【エラー】Mysql2::Error: Duplicate entry ' ' for key ' '

Posted at

環境

Ruby 3.0.2
Rails 6.1.4.1
omniauth-google-oauth2 1.0.0
devise 4.8.0

状況

Rspecを実行した際にエラーを吐かれた

 ActiveRecord::RecordNotUnique:
       Mysql2::Error: Duplicate entry '100000000000000000000' for key 'users.index_users_on_uid'
spec/factories/users.rb
FactoryBot.define do
  factory :user do
    name { 'test employee' }
    sequence(:email) { |n| "tester#{n}@example.com" }
    provider { 'google_oauth2' }
    uid { '100000000000000000000' }
  end
end

解決法

uidがかぶっていたのでエラーが出ていた。sequenceを使って一意にする

spec/factories/users.rb
FactoryBot.define do
  factory :user do
    name { 'test employee' }
    sequence(:email) { |n| "tester#{n}@example.com" }
    provider { 'google_oauth2' }
    sequence(:uid) { |n| "1#{n}00000000000000000000" }
  end
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?