开发者

Rails: oracle set_sequence_name being ignored

开发者 https://www.devze.com 2023-03-31 06:36 出处:网络
I have a simple model which I have to set the database name manually on. Also since it is using an oracle database, I\'m setting the sequence name so开发者_运维知识库 I can have auto incrementing id\

I have a simple model which I have to set the database name manually on.

Also since it is using an oracle database, I'm setting the sequence name so开发者_运维知识库 I can have auto incrementing id's.

When I run the rails console and try to create my model, it comes back and says that the sequence cannot be found. The weird part is the sequence it cannot find is not the sequence that I set in set_sequence_name.

Model

class Survey < ActiveRecord::Base
  set_sequence_name "SURVEY.SQ_SURVEY_ID"
  set_table_name "SURVEY.SURVEYS"
end

Console error

ActiveRecord::StatementInvalid: ActiveRecord::JDBCError: ORA-02289: 
sequence does not exist: select SURVEY.SURVEYS_seq.nextval id from dual

It looks like its ignoring my set sequence name line.

Am I just missing something?


FWIW: Using 11g I got away with:

self.id = ActiveRecord::Base.connection.execute("select SURVEY.SQ_SURVEY_ID.nextval id from dual").fetch

It appears that in my case, the sequence returns a cursor on which I need to do a fetch on.

11g/Rails 3.1


Clarifying, this works for oracle 10g

So as far as I can tell, this is a bug in the jdbc adapter (see here http://kenai.com/jira/browse/ACTIVERECORD_JDBC-133). For a work around I'm setting the id manually with a before create filter like this:

class Survey < ActiveRecord::Base
  set_table_name "SURVEY.SURVEYS"

  before_create do
    #since we can't use the normal set sequence name we have to set the primary key manually
    #so the execute command return an array of hashes,
    #so we grab the first one and get the nextval column from it and set it on id
    self.id = ActiveRecord::Base.connection.execute("select SURVEY.SQ_SURVEY_ID.nextval id from dual")[0]["id"]
  end

end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号