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

…衆ロ難τιáo~ 提交于 2019-12-19 05:45:14

问题


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:


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.



来源:https://stackoverflow.com/questions/3220473/auto-increment-a-non-primary-key-field-in-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!