Chapter 10.2.2. Automatic DB Schema Creation from SQLObject-Derived Classes

TurboGears provides a tg-admin sql create command, which we used to create the tables from the model class automatically.

But this is just a wrapper around functionality provided by SQLObject itself. This means that if you want to create tables programmatically from within your application, you can. Just define a class with some columns, call createTable(), and it will be created in the database. Here is how it’s done:

class FighterRobot(SQLObject): name = StringCol() weapon_1 = StringCol() weapon_2 = StringCol() engine = StringCol(default='Basic engine') health = IntCol(default=100) FighterRobot.dropTable(ifExists=True) # drop previous definition if exists FighterRobot.createTable() ...

Get Rapid Web Applications with TurboGears: Using Python to Create Ajax-Powered Sites now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.