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

**URL:** <https://www.ruby-forum.com/t/solved-re-postgresql-primary-sequence-key-issue-ruby-ra/87380>\
**Category:** Rails\
**Created:** [March 2, 2007, 4:23am UTC](https://www.ruby-forum.com/t/solved-re-postgresql-primary-sequence-key-issue-ruby-ra/87380 "2007-03-02T04:23:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![akm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@akm](https://www.ruby-forum.com/u/akm)\
**Post date:** [March 2, 2007, 4:23am UTC](https://www.ruby-forum.com/t/solved-re-postgresql-primary-sequence-key-issue-ruby-ra/87380/1 "2007-03-02T04:23:28Z")

</div>

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

–  
Regards

Andrew

---

<div class="post-metadata">

**Author:** ![akm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@akm](https://www.ruby-forum.com/u/akm)\
**Post date:** [March 2, 2007, 4:55am UTC](https://www.ruby-forum.com/t/solved-re-postgresql-primary-sequence-key-issue-ruby-ra/87380/2 "2007-03-02T04:55:29Z")

</div>

On 3/1/07, Andrew M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

How?

Curious,  
jeremy

–

---

<div class="post-metadata">

**Author:** ![akm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@akm](https://www.ruby-forum.com/u/akm)\
**Post date:** [March 2, 2007, 6:05pm UTC](https://www.ruby-forum.com/t/solved-re-postgresql-primary-sequence-key-issue-ruby-ra/87380/3 "2007-03-02T18:05:52Z")

</div>

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
