SOLVED: Re: PostgreSQL primary (sequence) key issue (Ruby/Ra


#1

Hi,
this one has been finally put to bed!!


Regards

Andrew


#2

On 3/1/07, Andrew M. removed_email_address@domain.invalid wrote:

Hi,
this one has been finally put to bed!!

How?

Curious,
jeremy


#3

Hi,

How?

the process is as follows (i’m using pgRails by the way):

  1. In project/config/environment.rb place the following line:

Include your application configuration below - Located at bottom of

page
ActiveRecord::Base.pluralize_table_names = false

  1. In C:\pgRails\lib\ruby\gems\1.8\gems\activerecord-
    1.15.2\lib\active_record\connection_adapters\postgresql_adapter.rb place
    the
    following code:

Note: You are looking for:

module ConnectionAdapters

class PostgreSQLAdapter < AbstractAdapter

then place the following code:

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def prefetch_primary_key?(table_name = nil)
    true
  end

  def next_sequence_value(sequence_name)
    Integer(select_value("SELECT nextval('#{sequence_name}')"))
  end

 This def next_sequence_value, alternatively, would be place in your

MySQL adapter file if you were using MySQL!!
#def next_sequence_value(sequence_name)
# sql = “UPDATE #{sequence_name} SET Id=LAST_INSERT_ID(Id+1);”
# update(sql, “#{sequence_name} Update”)
# select_value(“SELECT Id from #{ sequence_name}”,‘Id’)
#end

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1. In C:\pgRails\lib\ruby\gems\1.8\gems\activerecord-
    1.15.2\lib\active_record\base.rb place the following code:

Note: You are looking to replace/comment out def reset_sequence_name

then replace that block with this:

  def reset_sequence_name
    "#{table_name}_seq"
  end
  1. In your model class (for example)

class Usertbl < ActiveRecord::Base
set_primary_key “your_tbl_id”
set_sequence_name “your_db_sequence” #Note that your dB sequence
will
have to have the format of somename_seq
end

That’s it, ALL DONE!!

Hope that helps


Regards

Andrew