Auto increment a non-primary key field in Ruby on Rails

前端 未结 1 1487
耶瑟儿~
耶瑟儿~ 2021-01-12 01:33

In a RoR migration, how do I auto increment a non-primary-key field? I\'d like to do this in the db definition, and not in the model.

相关标签:
1条回答
  • 2021-01-12 02:21

    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)
    

    you can entry manually in your migration

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

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