1

我正在尝试使用 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

这是我收到的错误消息: OCIError: ORA-02289: sequence does not exist: INSERT INTO "OACM" ("ID_ACM","DESCR","GP_ACM_TYPE_FK","CHAN_FK","DT_START","DT_END","EXT_CODE_REF","FLG_BURN","LAYOUT_CODE","MAX_NUM","FLG_DPL","FLG_DEF","FLG_STATE","USR_INS","TS_INS","USR_DEL","TS_DEL","USR_UPD","TS_UPD","VER_NO","ORD_NO","CPCCCHK","TS_DPL") VALUES(id_acm.nextval,'DESCR','001','001',TO_DATE('01-APR-10','YYYY-MM-DD HH24:MI:SS'),TO_DATE('30-APR-11','YYYY-MM-DD HH24:MI:SS'),'','0','2',0,'3','','0',NULL,NULL,NULL,NULL,3,TO_TIMESTAMP('13-APR-11 18:21:24','YYYY-MM-DD HH24:MI:SS:FF6'),0,NULL,NULL,TO_TIMESTAMP('3','YYYY-MM-DD HH24:MI:SS:FF6')) (ActiveRecord::StatementInvalid)

谢谢

4

1 回答 1

2

对于它的价值,我对 ActiveRecord文档和网络上的其他注释的阅读是不可能的。Oracle 适配器使用并需要一个序列来生成 id,更重要的是,ActiveRecord 不允许id从外部设置它的值。您看到的错误是id_acm.nextval假设id_acm是一个序列的 SQL 表达式的结果。我认为 ar-extensions gem 可以做的任何事情都无法解决这种情况。

于 2013-06-27T10:47:10.870 回答