【发布时间】:2013-11-09 19:24:12
【问题描述】:
当我尝试将friendly_id 设置到我的rails4 项目similarly 时,在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
【问题讨论】:
-
你能告诉我们你的迁移吗?
-
@basgys 问题已更新。
标签: ruby-on-rails ruby-on-rails-4 friendly-id