Forum: Ruby on Rails nonstandard postgresql sequence names

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.
Dan T. (Guest)
on 2005-12-22 21:50
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?
Rick O. (Guest)
on 2005-12-22 22:05
(Received via mailing list)
> (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/...

http://rails.rubyonrails.com/classes/Fixtures.html#M000032

--
rick
http://techno-weenie.net
Dan T. (Guest)
on 2005-12-23 03:50
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)
>
> 
http://rails.rubyonrails.com/classes/ActiveRecord/...

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?


>
> http://rails.rubyonrails.com/classes/Fixtures.html#M000032
>
> --
> rick
> http://techno-weenie.net


D
This topic is locked and can not be replied to.