Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10072 (new defect)

Opened 2 years ago

Last modified 2 years ago

Migrations and PostgreSQL Primary Keys

Reported by: hunkybill Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: 1.2.3
Severity: normal Keywords:
Cc: rubyruy

Description

I use PostgreSQL and Migrations and never used to have any problems. Now, I cannot create a migration without the migration puking because of a faulty definition.

class CreateFoobars < ActiveRecord::Migration
  class Foobar < ActiveRecord::Base; end
  def self.up
    create_table :foobar do |t|
         t.column :name,     :string,        :null => false
    end
  end

  def self.down
    drop_table :foobar
  end
end

When I run rake it pukes with this explanation:

rake aborted!
PGError: ERROR:  multiple default values specified for column "id" of table "foobar"
: CREATE TABLE foobar ("id" serial primary key DEFAULT NULL, "name" character varying(255) NOT NULL) 

so.. trying to create a serial column with DEFAULT NULL is obviously a problem, but it never used to be. I can only speculate that at some point I did a GEM update or something and grabbed some new RAILS code that breaks PostgreSQL support? Not sure...but the only workaround seems to be defining an id column of NOT NULL... something that works but should not be necessary. Any tips on this most appreciated.

Thanks

Change History

11/05/07 00:49:09 changed by tarmo

Are you using any plugins which affect ActiveRecord? Or does this happen with 1.2.5 not edge?

It seems to work fine here, on rails edge, generating: "CREATE TABLE foobar ("id" serial primary key, "name" character varying(255) NOT NULL)"

11/05/07 01:52:31 changed by hunkybill

  • version changed from edge to 1.2.3.

Hi,

I am referring to a configuration of:

postgres (0.7.1)
    The extension library to access a PostgreSQL database from Ruby.

rails (1.2.3)
    Web-application framework with template engine, control-flow layer,
    and ORM.

I do use some plugins. Specifically: - upload_column, - active-rbac (revision 321) - composite-primary-keys (0.8.4) - composite-migrations (revision 411)

I see your definition does not try and quality the serial column with DEFAULT NULL. Not sure why my configuration is. A migration with no composite keys should not be affected by the presence of composite migrations or composite primary keys.. but I suppose I can try a migration with those plugins disabled to see if it makes a difference.

(follow-up: ↓ 4 ) 11/05/07 15:39:00 changed by cherbst

Does the pgsql sequence exist already?

(in reply to: ↑ 3 ) 11/05/07 17:15:52 changed by hunkybill

Replying to cherbst:

Does the pgsql sequence exist already?

No. The migration runs with an error with no sequence or table matching the one created in the migration.

11/07/07 20:52:21 changed by rubyruy

  • cc set to rubyruy.

http://qaix.com/postgresql-database-development/417-671-multiple-default-values-specified-for-column-read.shtml

Is this the problem? If it is, the postgres adapter probably needs to change. In the mean time, rolling back to postgresql 8.2.3 might work (the last version of pg i know worked), but heck if i can figure out how to roll back postgresql using mac ports. Sigh...

11/07/07 21:01:06 changed by rubyruy

And FYI, i'm having the exact same error trying to migrate to a blank database (from a fresh install). The error also occurs when using db:schema:load so it's not just migration specific.

11/07/07 22:01:25 changed by rubyruy

Ok, migrations work fine with the following postgres: PostgreSQL 8.2.4 on i686-pc-mingw32, compiled by GCC gcc.exe (GCC) 3.4.2 (mingw-special)

They run into the above-described error with this postgres: PostgreSQL 8.2.5 on i386-apple-darwin9.0.0, compiled by GCC i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5465)

I believe it stems from a change in behavior in the postgresql server, probably in this patch: https://projects.commandprompt.com/public/pgsql/changeset/29422

So, the postgres driver should be updated to reflect this. Until then postgresql 8.2.5 should not be considered supported by activerecord.

11/08/07 02:42:37 changed by hunkybill

Wow! Excellent information. I am running 8.2.5 so there you go.. maybe that's all it is... begs the questions.. can I just re-compile the driver with 8.2.5 in place? What if 8.3 goes from Beta to Stable Release tomorrow and I upgrade to 8.3... is this driver going to always be a problem or is it in lockstep?

11/16/07 23:45:15 changed by rabar10

Seeing the same issue on 8.2.5 on WinXP. Peeked at the postgresql_adapter and tried fixing it but had no luck. Any updates from anyone else being successful where I've failed?

Uninstalling and going back to 8.2.4 in the meantime...

12/06/07 03:20:30 changed by rubyruy

I'm not sure why but this problem no longer happens for me. My only guess is that one of the updates to the portfile did it (since i've since reinstalled the port). Maybe somebody else can confirm by doing an update and re-install from mac ports?

It could also be that I reinstalled the postgres gem (wanted to set a different lib include dir) - but I hardly see why that would make a difference. I don't see any recent updates to the gem so it can't be that (i think).

Maybe an activerecord patch I didn't notice?

12/10/07 22:19:28 changed by gryn

Adding:

t.column :id, :primary_key, :null => false

to the migration, is probably the simplest way to deal with this defect.

I tried to find a good place to patch the code, but everywhere I did, I could easily imagine someone yelling, "you can't touch that"!

For instance, if scheme_definitions.rb's primary_key could set :null => false, then everyone should be happy, even other db's. Except, that of course you change all db's to 'NOT NULL' versus 'DEFAULT NULL'. I don't know why you'd want to say 'DEFAULT NULL' though, it seems vaguely wrong.

This seems to be still in rails 2.0 too, and is still in 1.2.6.

-Adam