Add Id column in a migration

后端 未结 3 1205
萌比男神i
萌比男神i 2020-12-18 19:09

I have a Rails app, where one of the models does not have the id column. Doing some research I found the migration that created it:

create_table         


        
相关标签:
3条回答
  • 2020-12-18 19:12

    You can either manually add the id column:

    add_column :table_name, :id, :primary_key
    

    or clear (or backup) your data, rollback to this migration, get rid of the option :id => false, and re-migrate.

    0 讨论(0)
  • 2020-12-18 19:18

    You already got your answer, but here is a one liner that does all in this case

    rails generate migration AddIdToModel id:integer
    

    Look at the syntax of migration file name AddColumnNameToTableName followed the column description. It will generate something like below

    class AddIdToModel < ActiveRecord::Migration
      def change
        add_column :models, :id, :integer
      end
    end
    

    Now you can change this line if you feel for anything else. and just run rake db:migrate.

    0 讨论(0)
  • 2020-12-18 19:31

    You don't need to mention :integer

    rails g migration add_id_to_model id:primary_key worked for me.

    0 讨论(0)
提交回复
热议问题