How to turn off auto_increment in Rails Active Record

后端 未结 7 1749
日久生厌
日久生厌 2020-12-10 13:00

Is it possible to create primary key without auto_increment flag in ActiveRecord?

I can\'t do

create table :blah, :id          


        
7条回答
  •  被撕碎了的回忆
    2020-12-10 13:34

      def change
        create_table :tablename do |t|
          # t.string :fieldname
        end
    
       change_column :tablename, :id, :bigint, auto_increment: false
     end
    

    Notice: Since Rails 5.1 default primary keys are bigint. http://www.mccartie.com/2016/12/05/rails-5.1.html

    If you want 4-byte key change :bigint to :integer

提交回复
热议问题