[SQLObject] Re: createTable returns NoneType error
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Martin d'A. <Mar...@s2...> - 2004-11-16 06:56:28
|
On Mon, 15 Nov 2004, Martin d'Anjou wrote: > Hi, > > I have this code where I want to pre-define classes for the tables, and > connect them at run time to the database, but it does not work. Here is > the code: > > #!/usr/bin/env python > > from sqlobject import * > > class Person(SQLObject): > # Connection always defined at runtime > name = StringCol(length=255,default=None) > > def main(db_name): > conn = connectionForURI('mysql://user:passwd@server/'+db_name) > trans = conn.transaction > Person.createTable(trans) > > if __name__ == '__main__': > main('test') > > % ./simple.py > Traceback (most recent call last): > File "./simple.py", line 15, in ? > main('yo') > File "./simple.py", line 12, in main > Person.createTable(trans) > File "/usr/local/lib/python2.3/site-packages/sqlobject/main.py", > line 973, in createTable > if ifNotExists and cls._connection.tableExists(cls._table): > AttributeError: 'NoneType' object has no attribute 'tableExists' > I found a solution: class Person(SQLObject): # Connection always defined at runtime name = StringCol(length=255,default=None) def main(db_name): conn = connectionForURI('mysql://user:passwd@server/'+db_name) Person._connection = conn Person.createTable(ifNotExists=True) if __name__ == '__main__': main('test') And it works. Martin |