[英]How to limit the number of INSERT records with Populator in Rails
我有一個表格,我試圖填寫該表格以最終創建一個漂亮的網絡圖。 其中一個表稱為“用戶”,另一個表稱為“關系”。 我想用大約10個條目填寫它們。 該關系是多個關系中的一個用戶。
我正在使用的模型是
ActiveRecord::Schema.define(version: 20150603200530) do
create_table "relationships", force: true do |t|
t.integer "source"
t.integer "target"
t.integer "value"
end
create_table "users", force: true do |t|
t.string "name"
t.integer "group"
end
end
我正在使用填充寶石。 很酷
關系表與用戶表相關聯,為Relationship.source = user.id。 到目前為止,我想在一個名為populate.rake(在lib / assets中)的文件中為“源”,“目標”和“值”生成介於1和10之間的隨機值。
namespace :db do
desc "Erase and fill database"
task :populate => :environment do
require 'populator'
require 'faker'
[User, Relationship].each(&:delete_all)
User.populate 100 do |user|
user.name = Populator.words(1..3).titleize
Relationship.populate 1..10 do |relationship|
relationship.source = 1..10
relationship.target = 1..10
relationship.value = 1..10
end
end
end
end
但是我得到了錯誤:
ActiveRecord::StatementInvalid: SQLite3::SQLException: too many terms in compound SELECT: INSERT INTO "relationships" ("id", "source", "target", "value") VALUES (1, 9, 10, 6).... etc.
該錯誤似乎來自SQLite,它限制了一個INSERT
語句可以插入多少條記錄。
您可以通過設置populator選項:per_query
來嘗試減少每個INSERT的記錄數:
User.populate(100, :per_query => 10)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.