【问题标题】:Error when running rake db:seed using Faker gem使用 Faker gem 运行 rake db:seed 时出错
【发布时间】:2018-01-30 09:37:06
【问题描述】:

我正在尝试使用 faker gem 播种我的seeds.rb 文件。我的gem文件中有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:似乎索引和用户、电子邮件、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

【问题讨论】:

  • 您可以发布您的schema.rb 文件的内容吗?似乎unlock_token 列上有一个唯一索引,您不能将其留空。
  • 现在编辑并添加羞辱.rb
  • schema.rb 文件已添加

标签: ruby-on-rails ruby activerecord faker


【解决方案1】:

正如 Magnuss 指出的,您在 unlock_token 上有 unique 约束。但根据schema.rb,您没有Usersunlock_token 列。

【讨论】:

    【解决方案2】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-17
      • 2014-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多