我正在尝试从json文件中将sqlite3 db植入项目。我有两个模型用户和登录名。
require 'json'
records = JSON.parse(File.read('db/people.json'))
records.each do |record|
User.create!(record.except('logins').merge('password' => 'encrypted password'))
end
records.each do |record|
Login.create!(record['logins'])
end
当我运行rails db:seed时,它成功为用户播种,然后在创建登录时失败,并出现以下错误 ActiveRecord :: RecordInvalid:验证失败:用户必须存在模式或我的种子脚本无法确定是哪个
ActiveRecord::Schema.define(version: 2020_03_30_164743) do
create_table "logins", force: :cascade do |t|
t.datetime "date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.index ["user_id"], name: "index_logins_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "city"
t.string "state"
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
end
这是我的源代码https://github.com/jslack2537/apiDemoApp的链接
答案 0 :(得分:2)
如果您在同一循环内编写登录名,则可能会起作用。
setTaskCompletedWithSnapshot