当我尝试设置friendly_id
到我的 rails4 项目时,类似地,在“朋友”之后添加“朋友”到friends
表格后出现错误。我该如何解决:
PG::UniqueViolation - ERROR: duplicate key value violates unique constraint "index_friends_on_slug"
DETAIL: Key (slug)=() already exists.
此外,这是我的文件问题可能基于:
# 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
更新:迁移文件:
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