在Ruby on Rails中自动递增非主键字段

在Ruby on Rails中自动递增非主键字段

问题描述:

在RoR迁移中,如何自动增加非主键字段?我想在数据库定义中,而不是在模型中。

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.

您需要执行SQL语句。

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

注意这只是一个例子。最终的SQL语句语法取决于数据库。

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