简体   繁体   中英

How to limit the number of INSERT records with Populator in Rails

I have a table I am trying to fill to eventually create a pretty network diagram. One of the tables is called User and the other is called relationships. I'd like to fill them out with about 10 entries each. The relationship is one user to many relationships.

The model I am using is

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

I am using the populate gem. It is very cool

The relationship table is related to the user table as relationship.source=user.id. I would like to generate randome values for 'source', 'target' and 'value' of between 1 and 10. in a file called populate.rake (in lib/assets) so far I have:

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

but I get the error:

ActiveRecord::StatementInvalid: SQLite3::SQLException: too many terms in compound SELECT: INSERT INTO "relationships" ("id", "source", "target", "value") VALUES (1, 9, 10, 6).... etc.

This error seems to come from SQLite which has a limit of how many records you can insert with one INSERT statement.

You can try to reduce the number of records per INSERT by setting the populator option :per_query :

User.populate(100, :per_query => 10)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM