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.
+3
A:
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.
Am I correct in assuming by your answer that Rails does not provide a way to do this without direct execution of a SQL statement? I would expect something like: `t.integer :auto_i, :auto_increment => true` to work (but it does not).
kingjeffrey
2010-07-10 19:41:24
yes Rails does not provide :auto_increment keyword in migrations
2010-07-10 22:29:55
And if you want to all keywords or other good things in Rails then . " rubymine IDE for ROR " would be so useful. I am using for last 6 months.
2010-07-10 22:32:13