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 ...

I already searched the web2py book and this forum for that issue, but I 
only found a recipe on how to Populate on Deployment 
(http://web2py.com/books/default/chapter/29/13/deployment-recipes#Populating-a-Database-on-Deployment),
 
not on Migration.

Any hints would be highly welcome!

Daniela


-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/8b862b56-eab8-475a-95f1-fc8a7cccdb74n%40googlegroups.com.

Reply via email to