Primary key non default name

Hi,

Is there a way when using scaffolding to name a primary key like tablename_id instead of the default id?

Thanks,

Kostas

You could do this:

script/generate scaffold player player_id:primary_key name:string

which generates a migration like this

class CreatePlayers < ActiveRecord::Migration def self.up

create_table :players do |t|
  t.primary_key :player_id
  t.string :name

  t.timestamps
end

end

def self.down drop_table :players end end

Regards,

Craig

Thanks for the answer. I run: script/generate scaffold reservation reservation_id:primary_key name:string

followed by

rake db:migrate

and I get the following error:

== 20080830212120 CreateReservations: migrating

Thanks for the answer. I run: script/generate scaffold reservation reservation_id:primary_key name:string

you need create_table :players, :id => false do |t| ...

so that rails doesn't try to create its primary key as well as your own.

Fred

Thanks for the answer. I did that and the migration completed fine. Bit when I am trying to view the site it doesn't show the edit, show, destroy links, but only the "new" one. And the Create new doesn't work.

Kostas

Thanks for the answer. I did that and the migration completed fine. Bit when I am trying to view the site it doesn't show the edit, show, destroy links, but only the "new" one. And the Create new doesn't work.

have you got set_primary_key in your model ?

Fred

The whole thing seems that has a problem with:

1)The params:id in the controller do I leave it as :id or do I write the new primary key, e.g. :reseravations_id

2)in the routes.rb

  map.connect ':controller/:action/:id'   map.connect ':controller/:action/:id.:format'

It cannot get route as it searches for :id

Kostas

The whole thing seems that has a problem with:

1)The params:id in the controller do I leave it as :id or do I write the new primary key, e.g. :reseravations_id

should be able to leave it as :id - the primary key is always
available as id (ie the id method always exists and will return the
correct value)

Fred

Yes, the primary key is set when I use reservation_id:primary key when calling the generate script After db:migrate it shows as primary key on the database.

Kostas

Out of sheer curiosity, what's the compelling reason to ditch Rails' convention and change the primary key if you're still using scaffolding?

RSL

Well, you see this problem remains even when I use generate model and then manually fill the migration with t.primary_key:tablename_id It tries to create a separate id. And if I use :id =>false, so it creates only the tablename_id, I have the same problems as I mentioned above.

Kostas

I meant, what's the compelling reason to not go with Rails' defaults and use the :id field? If you're not using a legacy database and all. Most people who need/want to change the primary key are doing so because they are having to support a legacy db with columns already created. You seem to be creating this database for the first time and it's usually a good idea to go with Rails' defaults then, unless there's a compelling reason not to. Which is what I was asking. :slight_smile:

RSL

Oh right,

I am cerating the database for the first time, but I am trying to imitate an existing database (for testing purposes). Because the old app works with the existing database and unfortunately parts of the old app will continue to exist for some time alongside the new components that will be done with RoR.Messy, I know, but not my decision!!

Thanks,

Kostas