# Resetting sequences in unit tests with pg

**URL:** <https://www.ruby-forum.com/t/resetting-sequences-in-unit-tests-with-pg/53112>\
**Category:** Rails\
**Created:** [January 25, 2006, 6:00pm UTC](https://www.ruby-forum.com/t/resetting-sequences-in-unit-tests-with-pg/53112 "2006-01-25T18:00:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 25, 2006, 6:00pm UTC](https://www.ruby-forum.com/t/resetting-sequences-in-unit-tests-with-pg/53112/1 "2006-01-25T18:00:31Z")

</div>

Hi all,

Rails 1.0  
Postgresql 8.1  
Ruby 1.8.4

Is there a way to reset sequences in unit tests when loading fixture  
data? I  
came across mention of a "Fixtures.reset\_sequences method here:

[http://api.rubyonrails.com/files/vendor/rails/activerecord/CHANGELOG.html](http://api.rubyonrails.com/files/vendor/rails/activerecord/CHANGELOG.html)

However, when I tried to call Fixtures.reset\_sequences in the setup  
method, I  
got a NoMethodError.

Any ideas?

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 25, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/resetting-sequences-in-unit-tests-with-pg/53112/2 "2006-01-25T18:27:51Z")

</div>

Daniel B. wrote:

> However, when I tried to call Fixtures.reset\_sequences in the setup  
> method, I got a NoMethodError.
> 
> Any ideas?
> 
> Thanks,
> 
> Dan

A little further research indicates that the self.create\_fixtures method  
in  
fixtures.rb does this:

if connection.respond\_to?(:reset\_pk\_sequence!)  
table\_names.each do |table\_name|  
connection.reset\_pk\_sequence!(table\_name)  
end  
end

However, reset\_pk\_seqence! in the postgresql adapter has this comment:

# Resets sequence to the max value of the table’s pk if present.

So…if I want to reset the pk to the min, I’ll have to do that  
manually.

Any reason this isn’t set to the minimum in the first place? Just  
curious.

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 25, 2006, 6:43pm UTC](https://www.ruby-forum.com/t/resetting-sequences-in-unit-tests-with-pg/53112/3 "2006-01-25T18:43:04Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Jan 25, 2006, at 9:24 AM, Daniel B. wrote:

> However, reset\_pk\_seqence! in the postgresql adapter has this comment:
> 
> # Resets sequence to the max value of the table’s pk if present.
> 
> So…if I want to reset the pk to the min, I’ll have to do that  
> manually.
> 
> Any reason this isn’t set to the minimum in the first place? Just  
> curious.

To account for fixtures inserted without incrementing the sequence  
(since  
the fixture may specify a primary key.)

jeremy  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFD17glAQHALep9HFYRAuWKAJsFrz6xd6ST7jN5KGItM6JBwLTD0gCfVYpW  
bY744RktgL5Wb2iL3qk1GF4=  
=DNo1  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 25, 2006, 6:52pm UTC](https://www.ruby-forum.com/t/resetting-sequences-in-unit-tests-with-pg/53112/4 "2006-01-25T18:52:09Z")

</div>

Jeremy K. wrote:

> > manually.
> > 
> > Any reason this isn’t set to the minimum in the first place? Just  
> > curious.
> 
> To account for fixtures inserted without incrementing the sequence (since  
> the fixture may specify a primary key.)
> 
> jeremy

Ah, thanks Jeremy. Makes sense.

Dan
