Thread: [SQLObject] Strange bug with SQLObject 0.6
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Carlos R. <car...@gm...> - 2004-10-15 21:10:32
|
Hello all. I'm writing a prototype using sqlobjects and sqlite, and I've run into a strange problem. It may be a dumb mistake of mine (as I often do :-), but I was not able to spot it. This is the test code: ----- from sqlobject import connectionForURI, SQLObject, StringCol, IntCol, \ FloatCol, ForeignKey, MultipleJoin db = connectionForURI('sqlite:/work/test.db') class dbWorkItem(SQLObject): _connection = db wi_name = StringCol(length = 60, notNone = True) wi_description = StringCol(length = 200, notNone = True, default = '') wi_form_class = StringCol(length = 40, notNone = True) wi_form_id = IntCol(default=0) owner = ForeignKey('dbUser') workflow = ForeignKey('dbWorkFlow') class dbUser(SQLObject): _connection = db user_nickname = StringCol(length = 15, notNone = True, alternateID = True) user_password = StringCol(length = 15, notNone = True, default = '') user_name = StringCol(length = 40, notNone = True, default = '') user_address1 = StringCol(length = 40, notNone = False, default = '') user_address2 = StringCol(length = 40, notNone = False, default = '') user_city = StringCol(length = 40, notNone = False, default = '') user_comments = StringCol(length = 400, notNone = False, default = '') workitems = MultipleJoin('dbWorkItem') def new_db(): dbUser.createTable() dbWorkItem.createTable() dbUser(user_nickname="cribeiro", user_name="Carlos Ribeiro") dbWorkItem(wi_name='task1', wi_description='Task 1', wi_form_class='', workflow=None, owner=1) dbWorkItem(wi_name='task2', wi_description='Task 2', wi_form_class='', workflow=None, owner=1) >>> new_db() >>> u = dbUser.get(1) >>> u <dbUser 1 user_address1='' user_password='' user_address2='' user_city='' user_name='Carlos Ribeiro' user_nickname='cribeiro' user_comments=''> >>> u.workitems Traceback (most recent call last): File "<interactive input>", line 1, in ? AttributeError: 'dbUser' object has no attribute 'workitems' >>> u.user_name 'Carlos Ribeiro' >>> wi = dbWorkItem.get(1) >>> wi <dbWorkItem 1 wi_form_class='' wi_form_id=0 wi_name='Entra nota fiscal' workflowID=None ownerID=1 wi_description='Entra nota fiscal'> >>> wi.owner <dbUser 1 user_address1='' user_password='' user_address2='' user_city='' user_name='Carlos Ribeiro' user_nickname='cribeiro' user_comments=''> >>> wi.owner is u True For some reason, I cant access the dbUser.workitems property. I tried the example provided in the documentation (using Person and Address entities), and it works. What am I missing here? -- Carlos Ribeiro Consultoria em Projetos blog: http://rascunhosrotos.blogspot.com blog: http://pythonnotes.blogspot.com mail: car...@gm... mail: car...@ya... |
From: Michel T. <mic...@ya...> - 2004-10-16 03:28:14
|
Hi Carlos! I faced the same problem a day ago. This is a bug with Joins, you may pass a joinMethodName argument to solve this problem in your version, or update to a newer version from svn. I don't try the newer version, but, as Andrew Bennetts suggests me, I will try this weekend :) I hope I helped you, if I don't, you can mail me in portuguese (you are from python-brasil, aren't you?)... seeya --- Carlos Ribeiro <car...@gm...> escreveu: > Hello all. I'm writing a prototype using sqlobjects and sqlite, and > I've run into a strange problem. It may be a dumb mistake of mine (as > I often do :-), but I was not able to spot it. This is the test code: > > ----- > from sqlobject import connectionForURI, SQLObject, StringCol, IntCol, > \ > FloatCol, ForeignKey, MultipleJoin > > db = connectionForURI('sqlite:/work/test.db') > > class dbWorkItem(SQLObject): > _connection = db > wi_name = StringCol(length = 60, notNone = True) > wi_description = StringCol(length = 200, notNone = True, default > = '') > wi_form_class = StringCol(length = 40, notNone = True) > wi_form_id = IntCol(default=0) > owner = ForeignKey('dbUser') > workflow = ForeignKey('dbWorkFlow') > > class dbUser(SQLObject): > _connection = db > user_nickname = StringCol(length = 15, notNone = True, > alternateID = True) > user_password = StringCol(length = 15, notNone = True, default = > '') > user_name = StringCol(length = 40, notNone = True, default = > '') > user_address1 = StringCol(length = 40, notNone = False, default > = '') > user_address2 = StringCol(length = 40, notNone = False, default > = '') > user_city = StringCol(length = 40, notNone = False, default > = '') > user_comments = StringCol(length = 400, notNone = False, default > = '') > workitems = MultipleJoin('dbWorkItem') > > def new_db(): > dbUser.createTable() > dbWorkItem.createTable() > dbUser(user_nickname="cribeiro", user_name="Carlos Ribeiro") > dbWorkItem(wi_name='task1', wi_description='Task 1', > wi_form_class='', workflow=None, owner=1) > dbWorkItem(wi_name='task2', wi_description='Task 2', > wi_form_class='', workflow=None, owner=1) > > >>> new_db() > >>> u = dbUser.get(1) > >>> u > <dbUser 1 user_address1='' user_password='' user_address2='' > user_city='' user_name='Carlos Ribeiro' user_nickname='cribeiro' > user_comments=''> > >>> u.workitems > Traceback (most recent call last): > File "<interactive input>", line 1, in ? > AttributeError: 'dbUser' object has no attribute 'workitems' > >>> u.user_name > 'Carlos Ribeiro' > >>> wi = dbWorkItem.get(1) > >>> wi > <dbWorkItem 1 wi_form_class='' wi_form_id=0 wi_name='Entra nota > fiscal' workflowID=None ownerID=1 wi_description='Entra nota fiscal'> > >>> wi.owner > <dbUser 1 user_address1='' user_password='' user_address2='' > user_city='' user_name='Carlos Ribeiro' user_nickname='cribeiro' > user_comments=''> > >>> wi.owner is u > True > > For some reason, I cant access the dbUser.workitems property. I tried > the example provided in the documentation (using Person and Address > entities), and it works. What am I missing here? > > -- > Carlos Ribeiro > Consultoria em Projetos > blog: http://rascunhosrotos.blogspot.com > blog: http://pythonnotes.blogspot.com > mail: car...@gm... > mail: car...@ya... > > > ------------------------------------------------------- > This SF.net email is sponsored by: IT Product Guide on > ITManagersJournal > Use IT products in your business? Tell us what you think of them. > Give us > Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find > out more > http://productguide.itmanagersjournal.com/guidepromo.tmpl > _______________________________________________ > sqlobject-discuss mailing list > sql...@li... > https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss > ===== -- Michel Thadeu Sabchuk Curitiba/PR _______________________________________________________ Yahoo! Acesso Grátis - Internet rápida e grátis. Instale o discador agora! http://br.acesso.yahoo.com/ |
From: Carlos R. <car...@gm...> - 2004-10-18 12:07:58
|
Hello all, I just update my sqlobject installation, but I'm still having problems with MultipleJoins. This time the problem seems to be a little easier to diagnose, given the result of my test session: >>> execfile('test_sqlobj.py') >>> new_db() >>> u = dbUser.get(1) >>> u <dbUser 1 user_address1='' user_password='' user_address2='' user_city='' user_name='Carlos Ribeiro' user_nickname='cribeiro' user_comments=''> >>> u.workitems Traceback (most recent call last): File "<interactive input>", line 1, in ? File "<string>", line 1, in <lambda> File "C:\Python23\Lib\site-packages\sqlobject\joins.py", line 127, in performJoin inst.id) File "C:\Python23\Lib\site-packages\sqlobject\dbconnection.py", line 408, in _SO_selectJoin return self.queryAll("SELECT %s FROM %s WHERE %s = %s" % File "C:\Python23\Lib\site-packages\sqlobject\dbconnection.py", line 217, in queryAll return self._runWithConnection(self._queryAll, s) File "C:\Python23\Lib\site-packages\sqlobject\dbconnection.py", line 125, in _runWithConnection val = meth(conn, *args) File "C:\Python23\Lib\site-packages\sqlobject\dbconnection.py", line 210, in _queryAll self._executeRetry(conn, c, s) File "C:\Python23\Lib\site-packages\sqlobject\dbconnection.py", line 196, in _executeRetry return cursor.execute(query) File "C:\Python23\Lib\site-packages\sqlite\main.py", line 244, in execute self.rs = self.con.db.execute(SQL) DatabaseError: no such column: db_user_id It seems that the sqlobject code that 'mangles' the column names is trying to convert the 'uppercase-ID' convention that is used for automatic primary keys to the 'underscore' convention. I'm not yet familiar with sqlobject internals, though -- I'll attempt to fix it myself, but no promises :-) (btw, it seems that the problem was triggered by my own naming convention. I'll just rename all entities and see what happens) The script that defines the database is as follows: --------------- from sqlobject import connectionForURI, SQLObject, StringCol, IntCol, \ FloatCol, ForeignKey, MultipleJoin db = connectionForURI('sqlite:/work/test.db') class dbWorkItem(SQLObject): _connection = db wi_name = StringCol(length = 60, notNone = True) wi_description = StringCol(length = 200, notNone = True, default = '') wi_form_class = StringCol(length = 40, notNone = True) wi_form_id = IntCol(default=0) owner = ForeignKey('dbUser') workflow = ForeignKey('dbWorkFlow') class dbUser(SQLObject): _connection = db user_nickname = StringCol(length = 15, notNone = True, alternateID = True) user_password = StringCol(length = 15, notNone = True, default = '') user_name = StringCol(length = 40, notNone = True, default = '') user_address1 = StringCol(length = 40, notNone = False, default = '') user_address2 = StringCol(length = 40, notNone = False, default = '') user_city = StringCol(length = 40, notNone = False, default = '') user_comments = StringCol(length = 400, notNone = False, default = '') workitems = MultipleJoin('dbWorkItem') def new_db(): dbUser.createTable() dbWorkItem.createTable() dbUser(user_nickname="cribeiro", user_name="Carlos Ribeiro") dbWorkItem(wi_name='task1', wi_description='Task 1', wi_form_class='', workflow=None, owner=1) dbWorkItem(wi_name='task2', wi_description='Task 2', wi_form_class='', workflow=None, owner=1) -- Carlos Ribeiro Consultoria em Projetos blog: http://rascunhosrotos.blogspot.com blog: http://pythonnotes.blogspot.com mail: car...@gm... mail: car...@ya... |
From: Carlos R. <car...@gm...> - 2004-10-18 12:50:55
|
I've found my 'bug', and it hasn't nothing to do with naming conventions. It was a mistake of mine: my one-to-many relationship used a different column name from the default one, and I was not aware that in this case I had to supply a joinedColumn parameter (in fact, another user pointed it out to me, but I misunderstood his point, and assumed that I just had to upgrade my SQLObject installation). Regarding this problem, I have a suggestion: add a remark on the "One-to-Many Relationships" section of the documentation. The relevant information does exist but it's buried down in the MultipleJoin reference; so when I replayed the example provided in the tutorial section it worked, but I was not aware of the option, until I read the reference. For example, something like this: http://www.sqlobject.org/docs/SQLObject.html: """ We get the backreference with addresses = MultipleJoin('Address'). When we access a person's addresses attribute, we will get back a list of all the Address objects associated with that person. An example: p = Person(firstName='John', lastName='Doe') print p.addresses #>> [] a1 = Address(street='123', city='Smallsville', state='IL', zip='50484', person=p) print [a.street for a in p.addresses] #>> ['123'] In this case, the relationship between the 'Person' entity and the 'Address.person' ForeignKey() column is detected and created with no need for any extra information -- the name is mapped automatically. However, if the name of the ForeignKey() column is different from the entity name which it relates with, a joinedColumn argument will be required. For example: <...> """ I found my mistake only after realizing that the join method had no other way to find the joined-to column :-) But it's easy to overlook it, because SQLObject abstracts very well from these 'implementation details'. That's why I believe a comment early in the documentation (or in a FAQ section) could be useful. -- Carlos Ribeiro Consultoria em Projetos blog: http://rascunhosrotos.blogspot.com blog: http://pythonnotes.blogspot.com mail: car...@gm... mail: car...@ya... |