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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Andrew M. (Guest)
on 2007-03-02 05:23
(Received via mailing list)
Hi,
this one has been finally put to bed!!

--
Regards

Andrew
Jeremy K. (Guest)
on 2007-03-02 05:55
(Received via mailing list)
On 3/1/07, Andrew M. <removed_email_address@domain.invalid> wrote:
>
> Hi,
> this one has been finally put to bed!!


How?

Curious,
jeremy


--
Andrew M. (Guest)
on 2007-03-02 19:05
(Received via mailing list)
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

2. 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

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

3. 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

4. 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
This topic is locked and can not be replied to.