使用Faker gem

时间:2017-08-22 07:52:37

标签: ruby-on-rails ruby activerecord faker

我正在尝试使用faker gem播种我的seeds.rb文件。我的宝石文件中有faker,我安装了它。这是我的seeds.rb文件:

require 'random_data'
require 'faker'

u = User.create(
  name: 'joe user',
  email: 'joe@user.com',
  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:似乎索引和用户,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

2 个答案:

答案 0 :(得分:0)

正如Magnuss指出的那样,uniqueunlock_token约束。但根据schema.rb,您unlock_token没有Users列。

答案 1 :(得分:0)

您需要整理数据库架构。我甚至不确定这是如何工作的:

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

有点难以理解你是如何进入这种数据库状态的,但是如果你的unlock_token表中实际上没有一个名为users的列,我建议你创建一个新的迁移并删除该唯一索引:

def up
  remove_index :index_users_on_unlock_token
end