Create missing auto increment attribute with rails migration

前端 未结 3 1555
攒了一身酷
攒了一身酷 2020-12-19 02:40

I\'m writing a migration to convert a non-rails app into the right format for rails - one of the tables for some reason does not have auto increment set on the id column. I

3条回答
  •  轮回少年
    2020-12-19 03:20

    You need to execute an SQL statement.

    statement = "ALTER TABLE `users` CHANGE `id` `id` SMALLINT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT"
    ActiveRecord::Base.connection.execute(statement)
    

    Note this is just an example. The final SQL statement syntax depends on the database.

提交回复
热议问题