"Name has already been taken" in RSpec/FactoryGirl with multiple associations
<p>I am trying to run a very basic spec test and it fails with the error "<em>Name has already been taken</em>". </p>

<p><strong>Update</strong> belongs to <strong>User</strong> who has many <strong>Roles</strong>.</p>

<p><strong>user</strong> model</p>

<pre><code># == Schema Information
#
# Table name: users
#
#  id                     :integer          not null, primary key
#  email                  :string           default(""), not null
#

FactoryGirl.define do
  factory :user_engineer, class: User do
    id 1
    email 'someone@somewhere.com'
    roles {[FactoryGirl.create(:engineer)]}
  end
end
</code></pre>

<p><strong>roles</strong> model</p>

<pre><code># == Schema Information
#
# Table name: roles
#
#  id          :integer          not null, primary key
#  name        :string
#  description :text
#

FactoryGirl.define do
  factory :engineer, class: Role do
    id 3
    name 'Engineer'
    description 'He is the chosen one'
  end
end
</code></pre>

<p><strong>updates</strong> model</p>

<pre><code># == Schema Information
#
# Table name: updates
#
#  id            :integer          not null, primary key
#  content       :text
#  user_id       :integer
#  ticket_id :integer
#

FactoryGirl.define do
  factory :update do
    content "This is a test update"
    association :user, factory: :user_engineer
  end
end
</code></pre>

<p><strong>update_spec.rb</strong></p>

<pre><code>require 'rails_helper'

RSpec.describe Update, type: :model do
  let(:update){ FactoryGirl.create :update }
  it { expect(update).to be_valid }
end
</code></pre>

<p>This is the error:</p>

<pre><code>Update
  example at ./spec/models/update_spec.rb:19 (FAILED - 1)

Failures:

  1) Update 
     Failure/Error: roles {[FactoryGirl.create(:engineer)]}

     ActiveRecord::RecordInvalid:
       Validation failed: Name has already been taken
</code></pre>

<p>How can I pass the tests?!</p>

<p><strong>EDIT:</strong> By adding the sequence line I was suggested, I get the following error after running <code>RAILS_ENV=test rake db:drop</code>:</p>

<pre><code>1) Update 
     Failure/Error: roles {[FactoryGirl.create(:engineer)]}

     ActiveRecord::RecordNotUnique:
       PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "roles_pkey"
       DETAIL:  Key (id)=(3) already exists.
       : INSERT INTO "roles" ("id", "name", "description", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"
</code></pre>