kingjeffrey
kingjeffrey

Reputation: 15280

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

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.

Upvotes: 9

Views: 7448

Answers (1)

user386660
user386660

Reputation: 501

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.

Upvotes: 9

Related Questions