Skip to content
Advertisement

Use another column as default value in migration

I am trying to add a new column to existing tableName table which has a column anotherColumn.

How should I get the value of the existing column into this new column?

Advertisement

Answer

The short answer is, you can’t: the defaultTo value can’t come from another column. However, if you’re just trying to have the default take place at the time of migration you could do this:

It should hopefully be obvious that this will not update new rows being inserted following the migration: for that you’d need a trigger, or to ensure that you covered it in your insert code.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement