Rails 3 Migration: Autoincrement on (Non-Primary-Key) Column?

If you really think you need this you could create a before_create filter in the model to check the last record attribute value and add 1 to it. Feels hacking though.

I agree -- it seems hackish. As to why I'd need such a thing: autoincrement isn't just for primary keys, in the DB world. In this case, I'm adding a series of steps, and don't really like having to explicitly number them, when Rails could do it for me.

I may need to write this gem. – sabat Aug 20 '11 at 3:01.

Normally auto-incrementing columns are implemented using database sequences. The advantage of using a sequence over calculating the next increment, is that getting the next value from a sequence is atomic. So if you have multiple processes creating new elements, the sequence will make sure your numbers are really unique.

Sequences can be used in postgresql, oracle, mysql, ... How to implement this, if you are using postgres for instance: select the next value from the sequence: Integer(Operator.connection. Select_value("SELECT nextval('#{sequence_name}')")) create a sequence: Operator.connection. Execute("CREATE sequence #{sequence_name}") set the start-value of a sequence : Operator.connection.

Execute("SELECT setval('#{sequence_name}', #{new_start_serial})") Hope this helps.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions