开发者

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

开发者 https://www.devze.com 2023-01-07 03:58 出处:网络
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.You need to execute an SQL statement.

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.


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

精彩评论

暂无评论...
验证码 换一张
取 消