Hi,
I need to add a new column with default values to an existing table.
In the model file, I add
SQLField('newcolumn', type='text', default= 'Default text' )
to the db.define_table('mytest', ...) command.
After migration, all newly inserted tuples get 'Default text' in the column 'newcolumn' and the SQLForms are pre-populated with that value, which is the desired behavior.
However, all existing tuples in the table have a NULL value in that column, which is not desired.
The sql.log shows that the migration command is:
ALTER TABLE "mytest" ADD "newcolumn" CHAR(512);
What I would need instead is:
ALTER TABLE "mytest" ADD "newcolumn" CHAR(512) DEFAULT "Default text";
So my question is: How do I populate existing tuples with default values when migrating?
Is there an elegant, web2py supported way or do I have to do it manually?
And if I have to do it manually: Is there a good recipe for it?
My current idea would be to turn migration off on
the productive system, perform migration on the testing system, copy and
edit the commands from the sql.log on the testing system and run them
on the DB system of the productive system (which is Postgres).
However, there might be better ways to do that ...
Any hints would be highly welcome!
Daniela