开发者

Non-Autoincrementing the default Rails :id field

开发者 https://www.devze.com 2023-04-01 08:15 出处:网络
How can I create a table where the my :id field will not auto-increment? Example table: cost_categories

How can I create a table where the my :id field will not auto-increment?

Example table:

cost_categories

id    description
--    -----------
94    Labor
101   Framing
750   Paint

Please Note: As far as my :id column goes, everything about it will be th开发者_高级运维e Rails default accept the fact that I don't want it to auto-increment.

I've seen a lot of answers where they suggest not doing this, but it doesn't make sense to have, say, a rails default :id field AND my own :cc_id field.

I'd really appreciate any assistance.


For mysql DB: In your migration file:

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

YOu should add your index:

add_index(:table_name, :id, :unique => true, :name => 'id_index')
0

精彩评论

暂无评论...
验证码 换一张
取 消