Nick Ginanto
Nick Ginanto

Reputation: 32120

Add default value to a migration of another column

Since the updated_at column looks at all attributes when updated I want to add a new_updated_at which I will decide when it updates.

The default value on insertion via migration I want it to be the current updated_at.

How do I set the default value of the migration to be the value of the same row but of another column?

Upvotes: 1

Views: 1390

Answers (1)

mu is too short
mu is too short

Reputation: 434585

I think you have to do the initialization by hand, a quick bit of SQL in your up migration will take care of it:

def up
    # Add the new column...
    connection.execute('update models set new_updated_at = updated_at')
end

You'll have to replace models with your real table name of course.

You might need to modify the generated migration and split it into separate up and down methods but that shouldn't be a problem. If you don't want to do that, write a separate migration to initialize new_updated_at and put the above connection.execute call into that migration's up method.

Upvotes: 6

Related Questions