Tuesday 15 May 2012

postgresql - How can I run two database commands at once with Rails ActiveRecord? -



postgresql - How can I run two database commands at once with Rails ActiveRecord? -

i'm trying direct load postgresql table in rails. connection established this:

rc = connection.raw_connection rc.exec("copy research stdin csv header")

here portion of code:

key,values in valuehash entry = standard.model_constant::attribute_hash[key.to_sym] puts "loading #{entry[:display]}..." values.each |value| id = activerecord::base.connection.execute("select nextval('research_id_seq'::regclass)").first['nextval'] line = [id, value, entry[:display], standard.id, now, now] until rc.put_copy_data( line.to_csv ) errorprinter.print " waiting connection writable..." sleep 0.1 end end end

the line begins id = fails. says:

activerecord::statementinvalid: pg::unabletosend: command in progress

i don't want write these values csv file, read in file. want send info memory right database. can't think of valid reason why couldn't run 2 connections same database against same table, figured must doing wrong. i've tried 2 raw connections, keeps giving me same connection pool.

the best way handle have database insert sequence numbers you, rather trying fetch , add together them.

assuming table defined like:

create table research ( id serial, value text, ... )

then table defined such if insert row, , not specify value id, default value sequence automatically. applies as re-create other form of inserting rows.

the trick create sure not supplying value. this, construction re-create command not include id column:

copy research(value,....) stdin csv header

ie. have specify list of fields supply info for, , order in supply it.

note if have header, not used on import determine columns nowadays or order. header discarded.

assuming changed re-create statement, drop id array of values building send database.

ruby-on-rails-3 postgresql activerecord rails-postgresql

No comments:

Post a Comment