While I'm trying to setup friendly_id
to my rails4 project, similarly, I got error after I add "friend" after "friend" to friends
table. How can I fix it:
PG::UniqueViolation - ERROR: duplicate key value violates unique constraint "index_friends_on_slug"
DETAIL: Key (slug)=() already exists.
In addition, here are my files the issue may be based on:
# app/models/friend.rb:
class Friend < ActiveRecord::Base
has_many :entries, dependent: :destroy
belongs_to :user
extend FriendlyId
friendly_id :candidates, use: [:slugged, :finders] # not :history here
def candidates
[
:first_name,
[:first_name, :last_name]
]
end
end
# db/schema.rb:
create_table "friends", force: true do |t|
t.string "first_name"
t.string "last_name"
t.text "address"
t.string "email"
t.string "phone"
t.string "slug"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "friends", ["slug"], name: "index_friends_on_slug", unique: true, using: :btree
add_index "friends", ["user_id"], name: "index_friends_on_user_id", using: :btree
UPDATE: migration file:
class CreateFriends < ActiveRecord::Migration
def change
create_table :friends do |t|
t.string :first_name
t.string :last_name
t.text :address
t.string :email
t.string :phone
t.string :slug
t.integer :user_id
t.timestamps
end
add_index :friends, :slug, unique: true
add_index :friends, :user_id
end
end