Re: [SQLObject] Altering/updating a database
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Timothy W. G. <tim...@si...> - 2011-03-05 08:30:13
|
On 04/03/2011 2:00 PM, Oleg Broytman wrote: > On Fri, Mar 04, 2011 at 12:49:50PM +0000, Timothy W. Grove wrote: >> It seems to me that there should be a simple way to add the attribute to >> the class if the column has been added already, or not if it has been >> added through updating > There is no a simple way. You have to create the table class without > the column, and then look into the database to determine if the column > is there. You can use the same mechanism SQLObject uses to implement > fromDatabase: connection.columnsFromSchema(tableName, soClass); the > result is a list of SOCol instances. Thanks for the advice, Oleg. I found a solution which may not be elegant, but it works for me! Instead of trying to work with two different class definitions I've just kept one which declares the column I wish to add to the database and run an update function which deletes the column from the class before adding it again to both the class and the database. My example follows. Best regards, Tim class SignEntry(SQLObject): explanatory_map = PickleCol() def SignEntryUpdate(): cols = [i.name for i in sqlhub.processConnection.columnsFromSchema("sign_entry", SignEntry)] if 'explanatoryMap' not in cols: SignEntry.sqlmeta.delColumn('explanatory_map') # I know this exists in the class definition SignEntry.sqlmeta.addColumn(PickleCol("explanatory_map"), changeSchema=True) |