views:

1332

answers:

4

I have a DB migration like so:

 class CreateParticipations < ActiveRecord::Migration
      def self.up
        create_table(:participations, :primary_key => 'Seat')  do |t|      
          t.integer :Seat
          t.string :Nickname
          t.string :Clan
          t.string :FirstName
          t.string :LastName
          t.string :Email
          t.boolean  :Payed

          t.timestamps
        end
      end

      def self.down
        drop_table :participations
      end
    end

Now, seat is created with an Auto increment. However, I do not want that. I want it without an auto increment. I will define Seat myself in my Logic.

I have been looking around but I cannot find how to disable auto_increment.

How do I do this? Except for manually doing it in MySQL.

+3  A: 

Are you really sure you want to do this? It goes against the convention that works very well throughout rails and will cause you lots of extra work in the rest of your application making this exception to the rule work everywhere. Perhaps "Seat" in just a field on this table with a unique index instead. Unless you need this to adapt to a legacy schema of some sort this is usually a bad idea.

Sadly I don't know the actual answer to your question.

Squeegy
+1  A: 

Is there a reason you can't use rails' id key and manually add an index called Seat?

I've seen some hacks just to get rails to -work- on non-increment-pk databases. I don't think it's an option. If I recall, that's how rails accesses all its per-row functionality.

Honestly, how -absolutely- do you need that slight boost in efficiency of ignoring rails' structure?

I think the real answer is "you can't." Activerecord has a few things it will not bend on.

I am porting an ASP.MVC application where I had my BLL and DAL custom logic, but it makes sense now
IceHeat
+2  A: 

For the record, if you absolutely need to do this (it shouldn't happen often), here's the way to do a non-autoincrementing primary key with the Rails migration DSL:

create_table(:table_name, :id => false) do |t|
  t.integer :id, :options => 'PRIMARY KEY'
end

That will work for MySQL anyway, if your DB uses different syntax to specify a primary key, replace the :options => 'PRIMARY KEY' with whatever that is

AdminMyServer
A: 

Since everyone seems to doubt this case could be justified, I would like to suggest a scenario when I think it makes sense. Please tell me if I am missing something or if it's bad design:

I have a table countries, and one of my model (User for example) has_one :country. In that case I use the table countries as a persistent enumeration, where I want Costa Rica to always have the same fixed ID even if I, for a reason or another, reinitialize this table.

Ok maybe a first alternative would be not to use an ID column at all, but rather an ISO code of countries as the primary key, but it's not standard rails either.

kilo