2017-08-22 10 views
0

seeds.rbファイルにfaker gemをシードしようとしています。私は私の宝石ファイルに偽者を持っています。私はそれをインストールしました。ここに私のseeds.rbファイルされる:Fake gemを使用してrake db:seedを実行するとエラーが発生する

require 'random_data' 
require 'faker' 

u = User.create(
    name: 'joe user', 
    email: '[email protected]', 
    password: 'password', 
    confirmed_at: Time.now 
) 

15.times do 
    User.create!(
    name: Faker::Name.name, 
    email: Faker::Internet.email, 
    password: Faker::Internet.password, 
    #confirmed_at: Faker::DateTime.dateTime($max = 'now', $timezone =  date_default_timezone_get()) 
    confirmed_at: Time.now 
) 
end 
users = User.all 

    50.times do 

    Wiki.create!(
    title: Faker::Name.title, 
    body: Faker::Lorem.text($maxNbChars = 400), 
    private: false, 
    user: user.sample 
) 
end 
wikis = Wiki.all 

puts "Seed finished" 
puts "#{Wiki.count} wikis created" 
puts "#{User.count} users created" 

私はrake db:seedを実行しようとすると、私はこのエラーを取得:

ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: index 'index_users_on_unlock_token': INSERT INTO "users" ("name", "email", "encrypted_password", "confirmed_at", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?)

私の問題は何ですか?

ここに私のschema.rbがあります:それはエラーが私に与えているものなので、indexとusers、email、confirmed_at、およびcreated_atに問題があるようです。

ActiveRecord::Schema.define(version: 20170817045146) do 

    create_table "users", force: :cascade do |t| 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.integer "role",     default: 0 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
t.datetime "remember_created_at" 
t.integer "sign_in_count",   default: 0, null: false 
t.datetime "current_sign_in_at" 
t.datetime "last_sign_in_at" 
t.string "current_sign_in_ip" 
t.string "last_sign_in_ip" 
t.string "confirmation_token" 
t.datetime "confirmed_at" 
t.datetime "confirmation_sent_at" 
t.string "unconfirmed_email" 
t.datetime "created_at",       null: false 
t.datetime "updated_at",       null: false 
t.string "name" 
    end 

    add_index "users", ["confirmation_token"], name:  "index_users_on_confirmation_token", unique: true 
    add_index "users", ["email"], name: "index_users_on_email", unique: true 
    add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 
    add_index "users", [nil], name: "index_users_on_unlock_token", unique: true 

    create_table "wikis", force: :cascade do |t| 
t.string "title" 
t.text  "body" 
t.boolean "private", default: false 
t.integer "user_id" 
t.datetime "created_at",     null: false 
t.datetime "updated_at",     null: false 
    end 

    add_index "wikis", ["user_id"], name: "index_wikis_on_user_id" 

end 
+1

'schema.rb'ファイルの内容を投稿できますか?カラム 'unlock_token'にユニークなインデックスがあり、それを空のままにすることはできません。 – Magnuss

+0

編集してshame.rbを追加する –

+0

schema.rbファイルが追加されました –

答えて

0

Magnussが指摘したように、あなたはunlock_tokenunique制約を持っています。しかしschema.rbによれば、Usersにはunlock_tokenの列はありません。

+0

ありがとう、私は両方を削除し、今すぐ動作します。 –

0

データベーススキーマをソートする必要があります。

add_index "users", [nil], name: "index_users_on_unlock_token", unique: true 

は、それはあなたがこのデータベースの状態になったかを理解するために少し難しいですが、あなたが実際にあなたのusers表にunlock_tokenという列を持っていない場合、私は:私はこれがどのように動作するかさえわかりません新しい移行の作成とユニークなインデックスの削除をお勧めします:

def up 
    remove_index :index_users_on_unlock_token 
end 
関連する問題