使用Ruby Activerecord扩展导入时禁用密钥自动生成

使用Ruby Activerecord扩展导入时禁用密钥自动生成,ruby,oracle,activerecord,csv,import,Ruby,Oracle,Activerecord,Csv,Import,我试图使用ar extensions gem将记录从CSV文件导入到Oracle表中,它通过序列生成键列的值,但该值在我的文件中,我需要它保持不变,有没有办法禁用键自动生成 这是我的代码,为Cucumber任务准备,该任务必须为测试加载初始数据: require 'rubygems' require 'active_record' require 'CSV' require 'ar-extensions' #Logger Objects are handy for finding ou

我试图使用ar extensions gem将记录从CSV文件导入到Oracle表中,它通过序列生成键列的值,但该值在我的文件中,我需要它保持不变,有没有办法禁用键自动生成

这是我的代码,为Cucumber任务准备,该任务必须为测试加载初始数据:

    require 'rubygems'
require 'active_record'
require 'CSV'
require 'ar-extensions'

#Logger Objects are handy for finding out why imports crash
logger = Logger.new('import.log')
#Set the Logger level to Info to prevent boring debug messages
logger.level = Logger::INFO

fileName  = " "
tableName = " "
validate = true

ActiveRecord::Base::establish_connection(
    :adapter=>"oracle_enhanced",
    :host=>"192.168.202.123",
    :port=>"1521",
    :database=>"XE",
    :username=>"xxx",
    :password=>"xxx")

#Connect Logger to Active Record
ActiveRecord::Base.logger = logger

class Oacm < ActiveRecord::Base

  self.table_name = "OACM"
  #self.set_primary_key "id_acm"
  self.set_sequence_name :id_acm
  attr_accessible :id_acm
  attr_accessible :descr
  attr_accessible :gp_acm_type_fk
  attr_accessible :chan_fk
  attr_accessible :dt_start
  attr_accessible :dt_end
  attr_accessible :ext_code_ref
  attr_accessible :flg_burn
  attr_accessible :layout_code
  attr_accessible :max_num
  attr_accessible :flg_dpl
  attr_accessible :flg_def
  attr_accessible :flg_state
  attr_accessible :usr_ins
  attr_accessible :ts_ins
  attr_accessible :usr_del
  attr_accessible :ts_del
  attr_accessible :usr_upd
  attr_accessible :ts_upd
  attr_accessible :ver_no
  attr_accessible :ord_no
  attr_accessible :cpccchk
  attr_accessible :ts_dpl
  attr_accessible :ts_dpl
end

def cpccchk_before_type_cast
  cpccchk
end

Given(/^a file named "(.*?)" containing all "(.*?)" data$/) do |arg1, arg2|
  fileName = arg1
  tableName = arg2
  #puts arg1.to_s << " " << arg2.to_s
end

Then(/^it should be loaded$/) do
  preparedRecord = []
  CSV.foreach(fileName, :headers => true) do |row|
    hashedRow = row.to_hash
    puts hashedRow
    #Oacm.id_acm = hashedRow["id_acm"]
    preparedRecord << Oacm.new(hashedRow)
    #puts  preparedRecord.to_s

  end
  #puts   preparedRecord.to_s
  Oacm.import preparedRecord, :validate => true
end
需要“rubygems”
需要“活动记录”
需要“CSV”
需要“ar扩展”
#Logger对象可以方便地找出导入崩溃的原因
logger=logger.new('import.log')
#将记录器级别设置为Info,以防止无聊的调试消息
logger.level=logger::INFO
fileName=“”
tableName=“”
验证=真
ActiveRecord::Base::建立\u连接(
:adapter=>“oracle_增强版”,
:host=>“192.168.202.123”,
:port=>“1521”,
:database=>“XE”,
:username=>“xxx”,
:密码=>“xxx”)
#将记录器连接到活动记录
ActiveRecord::Base.logger=记录器
类Oacm#把arg1.to_s放到它值得的地方,我对ActiveRecord的阅读和其他网络上的注释,比如说这是不可能的。Oracle适配器使用并需要一个序列来生成id,更重要的是,ActiveRecord不允许从外部设置其
id
的值。您看到的错误是由于
id\u acm.nextval
SQL表达式导致的,该表达式假定
id\u acm
是一个序列。我不认为ar extensions gem可以做的任何事情都可以避免这种情况。

即使我在发布之前做了大量的研究和测试,遗憾的是我认为你是对的