Attached is a patch that will automatically detect the sequence name for any given schema.table in PostgreSQL. by default, it will assume public is the SCHEMA, but will also split on a schema.table_name and detect the proper sequence_name in the new method sequence_name, which performs the following SQL query against the PostgreSQL system tables.
SELECT seq.relname::text
FROM pg_class src, pg_class seq, pg_namespace, pg_attribute, pg_depend
WHERE
pg_depend.refobjsubid = pg_attribute.attnum AND
pg_depend.refobjid = src.oid AND
seq.oid = pg_depend.objid AND
src.relnamespace = pg_namespace.oid AND
pg_attribute.attrelid = src.oid AND
pg_namespace.nspname = 'SCHEMANAME' AND
src.relname = 'TABLENAME' AND
pg_attribute.attname = 'PRIMARY KEY COLUMN';
This allows for full SCHEMA support such as:
CREATE SCHEMA foo;
CREATE TABLE foo.bars (id SERIAL, name VARCHAR(100));
The model would look like so:
class Foobar < ActiveRecord::Base
def self.table_name() "foo.bars" end
end
Within console:
$ ./script/console
Loading development environment.
>> Foobar.create(:name => 'Robby Russell')
=> #<Foobar:0x275558c @new_record=false, @attributes={"name"=>"Robby Russell", "id"=>1}, @errors=#<ActiveRecord::Errors:0x2751fcc @base=#<Foobar:0x275558c ...>, @errors={}>>
>> Foobar.create(:name => 'Someone Else')
=> #<Foobar:0x274df6c @new_record=false, @attributes={"name"=>"Someone Else", "id"=>2}, @errors=#<ActiveRecord::Errors:0x274cbd0 @base=#<Foobar:0x274df6c ...>, @errors={}>>
>> Foobar.find:all
=> [#<Foobar:0x27495c0 @attributes={"name"=>"Robby Russell", "id"=>"1"}>, #<Foobar:0x2749584 @attributes={"name"=>"Someone Else", "id"=>"2"}>]
>>