# Nonstandard postgresql sequence names

**URL:** <https://www.ruby-forum.com/t/nonstandard-postgresql-sequence-names/101228>\
**Category:** Rails\
**Created:** [December 22, 2005, 8:50pm UTC](https://www.ruby-forum.com/t/nonstandard-postgresql-sequence-names/101228 "2005-12-22T20:50:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dandante](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@dandante](https://www.ruby-forum.com/u/dandante)\
**Post date:** [December 22, 2005, 8:50pm UTC](https://www.ruby-forum.com/t/nonstandard-postgresql-sequence-names/101228/1 "2005-12-22T20:50:53Z")

</div>

I’m working with a legacy postgresql database where the names of tables,  
columns, etc., do not usually follow Rails conventions.

I’ve been able to work around it for the most part, but I ran into this:  
I have the following test:  
require File.dirname( **FILE** ) + ‘/…/test\_helper’

class SponsorTest \< Test::Unit::TestCase  
self.use\_transactional\_fixtures = true  
fixture :sponsors, :table\_name =\> “sponsor”  
fixture :eligibilities, :table\_name =\> “topic\_eligibility”

# Replace this with your real tests.

def test\_truth  
assert\_kind\_of Sponsor, sponsors(:first)  
end  
end

(The “fixture” method comes from a patch that allows me to specify  
non-standard table names–the table in this case is called sponsor, not  
sponsors).

The sponsor table’s primary key column is called “sponsor\_key” instead  
of id, and instead of it being a serial type, it is just an integer. It  
is incremented using a sequence named “sponsor\_seq”.

I also have a sponsors.yml fixture with unremarkable contents.  
When I try and run the test I get this:

Loaded suite test/unit/sponsor\_test  
Started  
EWARNING: there is no transaction in progress  
E  
Finished in 0.831961 seconds.

1. Error:  
test\_truth(SponsorTest):  
ActiveRecord::StatementInvalid: PGError: ERROR: relation  
“sponsor\_sponsor\_key\_seq” does not exist  
: SELECT setval(‘sponsor\_sponsor\_key\_seq’, (SELECT  
COALESCE(MAX(sponsor\_key), 0)+1 FROM sponsor), false)  
[…]

It looks like the code in fixture.rb hardcodes an assumption about  
sequence names. Any way that could be parameterized?

---

<div class="post-metadata">

**Author:** ![dandante](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@dandante](https://www.ruby-forum.com/u/dandante)\
**Post date:** [December 22, 2005, 9:05pm UTC](https://www.ruby-forum.com/t/nonstandard-postgresql-sequence-names/101228/2 "2005-12-22T21:05:47Z")

</div>

> (The “fixture” method comes from a patch that allows me to specify  
> non-standard table names–the table in this case is called sponsor, not  
> sponsors).

Why not just name your fixtures sponsor.yml and topic\_eligibility.yml?

> It looks like the code in fixture.rb hardcodes an assumption about  
> sequence names. Any way that could be parameterized?

It calls #pk\_and\_sequence\_for on each table while creating fixtures  
(Fixtures#create\_fixtures)

[http://rails.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/PostgreSQLAdapter.html#M000623](http://rails.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/PostgreSQLAdapter.html#M000623)

[http://rails.rubyonrails.com/classes/Fixtures.html#M000032](http://rails.rubyonrails.com/classes/Fixtures.html#M000032)

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![dandante](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@dandante](https://www.ruby-forum.com/u/dandante)\
**Post date:** [December 23, 2005, 2:50am UTC](https://www.ruby-forum.com/t/nonstandard-postgresql-sequence-names/101228/3 "2005-12-23T02:50:30Z")

</div>

Rick O. wrote:

> > (The “fixture” method comes from a patch that allows me to specify  
> > non-standard table names–the table in this case is called sponsor, not  
> > sponsors).
> 
> Why not just name your fixtures sponsor.yml and topic\_eligibility.yml?

OK, I did that. It’s still looking for a sequence called  
“sponsor\_sponsor\_key\_seq” when the sequence is actually called  
“sponsor\_seq”.

> > It looks like the code in fixture.rb hardcodes an assumption about  
> > sequence names. Any way that could be parameterized?
> 
> It calls #pk\_and\_sequence\_for on each table while creating fixtures  
> (Fixtures#create\_fixtures)
> 
> [Peak Obsession](http://rails.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/PostgreSQLAdapter.html#M000623)

I looked at the source for that method. I’m not clear exactly what it’s  
doing but it does seem to make the assumption there there is some  
derivable association between a table, its primary key, and the sequence  
name associated with that table.

I know that the SERIAL type creates a sequence called  
tablename\_primarykeyname\_seq, but unfortunately the database I am  
working with has sequence names that don’t necessarily match that  
convention (or any other). Assuming that I don’t have the option of  
changing the schema at all, what can I do here?

> [Peak Obsession](http://rails.rubyonrails.com/classes/Fixtures.html#M000032)
> 
> –  
> rick  
> [http://techno-weenie.net](http://techno-weenie.net)

D
