Disable autoincrement

Hello

I want to create a table with ID column but dont want auto increment
used. (columns in table are id and string) I am using mysql. Can you
help me write the create migration?

Why do you not want auto increment? If it is because you want control
of an id field for something like a product code or user number then
it is probably easier to let Rails use the normal auto increment id
field and add another one of your own, product_code or whatever it is,
that you can manipulate as you wish without having to go against the
Rails conventions. This is more likely to give you a peaceful life.

Colin

Yup thought of what you said already but dont like it because already
my app has become huge:
1. almost 14 reference tables and 10 app tables.
2. 4 -5 deployments

Every time I change some reference table with global impacts. I dont
want a new id because other application tables across all the
deployments might be using that particular reference table id. So I
want to fix up my reference table ids permanently by switching off
auto increment and taking control over it.

Of course app tables i will keep auto increment on.

This way any ref table change, I will write 1 migration to delete all
refs tables and rewrite them ... increasing the deployment window but
simplifying how to steps.

Yup thought of what you said already but dont like it because already
my app has become huge:
1. almost 14 reference tables and 10 app tables.
2. 4 -5 deployments

Do you mean that the table is an existing table in a legacy db? If so
then google rails legacy and you will find how to use an existing
field as the id. Even if it is not an existing table then sitll
google it as you will find answers to your problem there.

Every time I change some reference table with global impacts. I dont
want a new id because other application tables across all the
deployments might be using that particular reference table id. So I
want to fix up my reference table ids permanently by switching off
auto increment and taking control over it.

Do you mean you will completely rewrite the table using a migration or
similar, but wish to set specific id values? I believe in this case
it is possible to force id fields to particular values even though it
is nominally auto increment. Though I have not done it myself.

Colin

If you provide an id for a model, that id will be used.

ref = RefModel.new(fields) {|r| r.id = 42 }
ref.save

You can't assign the id in the attributes hash of the .new method because ActiveRecord automatically protects the id from mass-assignment. If you set the id in a block or in a separate statement, then it will be used (unless it causes a conflict at the database level because, for example, it has a unique index).

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#M001901

create_table(:suppliers, :id => false) do |t|
   t.column :name, :string, :limit => 60
   # Other fields here
end

:id => false is the key here