Re: [SQLObject] list index out of range?
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Luke O. <lu...@me...> - 2003-11-27 07:36:41
|
Mike - listofpersons (and any return of select()) is a list, not a dictionary keyed by ids. So listofpersons[newid] is almost guaranteed to be out of range (if your database ids start at 1 and list indexes start at 0, then even with no deleted Persons it will be wrong), and even if not out of range probably not the object you wanted. If you want a specific Person, use Person(newid), if you want a list of Persons from select() treat them as a list (ie, iterate through them as you mentioned works, or access by positional index, etc). - Luke Quoting Mike Edmonds <mi...@mi...>: > Can anyone tell me why I get the following error? I am creating a new > object, and then trying to access that object via a newly queried second > object. And getting a list index out of range error. If I simply iterate > through the new list (without referring to the item by index) then it will > appear in the list. If I run a different script with the same code querying > the table I can access it okay. > > > from SQLObject import * > > > class Person(SQLObject): > _connection = MySQLConnection(host='localhost', db='jcpos', > user='test', passwd='') > first_name = StringCol(length=100,default="") > middle_initial = StringCol(length=1, default="") > last_name = StringCol(length=100, default="") > > > newperson = Person.new(first_name="Mike") > newid = newperson.id > > listofpersons = Person.select() > print listofpersons[newid].first_name > > Traceback (most recent call last): > File "D:\dev\pythontest\sqlerror.py", line 18, in ? > print listofpersons[newid].first_name > File "C:\Python23\Lib\site-packages\SQLObject\SQLObject.py", line 1056, in > __g > etitem__ > return list(self.clone(start=value, stop=value+1))[0] > IndexError: list index out of range |