Nonstandard postgresql sequence names

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?

(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/Fixtures.html#M000032


rick
http://techno-weenie.net

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

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


rick
http://techno-weenie.net

D