ActiveRecord error with :id => false

I'm using the postgresql adapter with ActiveRecord. When I execute this script, I get an error about the id sequence not existing even though i created the table without the id.

require 'rubygems' require 'active_record'

class Article < ActiveRecord::Base end

ActiveRecord::Base.establish_connection(   :adapter => "postgresql",   :database => "mytestdb",   :host => "localhost",   :username => "xxxx",   :password => "xxxx" )

ActiveRecord::Schema.define(:version => 0) do   create_table :articles, :id => false do |t|     t.column :title, :string   end end

art = Article.create( :title => "Green Eggs and Ham" )

Executing this script gives this error:

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract_adapter.rb:120:in `log': RuntimeError: ERROR C42P01 Mrelation "articles_id_seq" does not exist Fnamespace.c L200 RRangeVarGetRelid: SELECT currval('articles_id_seq') (ActiveRecord::StatementInvalid)

Since I created the table with ":is => false", there is no articles_id_seq and so AR should not be looking for one. Am I missing something or is this a bug?

Thanks in advance, Nick

Even though what I posted to you works there may be a problem with ActiveRecord, by supplying the "primary_key" option it should have created a primary key in the database table for you, but the PostgreSQL adapter does not do this. It just creates an indexed and sequenced column.

It does for me... at least with rails 1.1.6, postgres 8.1.5, and the postgres 0.7.1 gem...

I just checked one of my tables and it reports a primary key...

otf_dev=> \d photos;                                        Table "public.photos"     Column | Type | Modifiers

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

Philip Hallstrom wrote:

Even though what I posted to you works there may be a problem with ActiveRecord, by supplying the "primary_key" option it should have created a primary key in the database table for you, but the PostgreSQL adapter does not do this. It just creates an indexed and sequenced column.

It does for me... at least with rails 1.1.6, postgres 8.1.5, and the postgres 0.7.1 gem...

I just checked one of my tables and it reports a primary key...

You have a "default" id generated primary key column. I am talking in reference to the OPs scenarios, where you have a different field then "id" be the primary key.

I agree it does work when using the default "id" primary key field.

Ah... I'd missed that part of the thread... I thought I'd read somewhere that if you specify your own primary key you're on your own as far as setting up the sequence/next id stuff regardless of DB since Rails assumed if you're doing that you had a good reason... but I coudl be wrong. And not sure it's even relavant to what you two were talking about :slight_smile: