web2py: How to rename a table?
How do I rename a database table in web2py? If there is not a direct way, what is the best workaround to do this? All I found was this thread http://groups.google.com/group/web2py/browse_thread/thread/ef724508324347e2/7966开发者_如何学运维a423c293bdec where the creator of web2py says he does not have an easy way.
To change the database schema while running web2py
db.executesql('ALTER TABLE old_name RENAME TO new_name;')
This will not change your code! Only you can change your code.
So, if you are only doing this once, say because you have an ugly or ambiguous table name and want to refactor your code, then it is likely best not to use web2py to change the table name in the database schema. Here is how I would do it.
Stop the application
Rename the table in the db schema using the sqlite3 console program, or whatever database management program you use instead. I guess this might be your real problem, because you are accustomed to using web2py as your database management program. Well, I guess you will have to learn how to use the sqlite3 console console program.
Change the code in your model
Restart the application.
However, if you really insist on using web2py only to manage your database, then something like this should work:
Create a new controller, say 'table_rename' Add the line
db.executesql('ALTER TABLE old_name RENAME TO new_name;')
to the controller
Brows to application/table_rename
Stop the application
Change your model code
Remove table_rename
Restart application.
精彩评论