Thread: [SQLObject] string, unicode and column id
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Luc M. <lu...@pt...> - 2008-07-17 09:34:23
|
Hello, I am discovering sqlobject with turbogears and I face a problem which may be sum up as follow : My model : (...) class Page(SQLObject): pagename = UnicodeCol(alternateID=True,unique=True,notNone=True,length=15) tabname = UnicodeCol(length=30) content = RelatedJoin("Content") sidebar = ForeignKey("Sidebar") (...) And in a console: >>> p = Page.get(1) >>> p <Page 1 pagename=u'home' tabname=u'home foo' sidebarID=None> >>> p.sidebar = 1 >>> p <Page 1 pagename=u'home' tabname=u'home foo' sidebarID=1> >>> p.sidebar = '1' >>> p <Page 1 pagename=u'home' tabname=u'home foo' sidebarID=1> >>> p.sidebar = u'1' >>> p <Page 1 pagename=u'home' tabname=u'home foo' sidebarID=None> As I populate the table from a web form, all values come as a unicode type. I catch all of them as a key/pair value and modify my "p" record in a loop with something like setattr(p,key,value). My problem is that setattr(p,'sidebar',u'1') does not raise any error but silently gives the None value to sidebarID, which force me to an ugly and unsecure workaround : if hasattr(p,key+'ID'): setattr(p,key+'ID',int(value)) This unicode problem does not affect RelatedJoin columns : >>> p.addContent(u'2') >>> p.content[1] <Content 2 title=u'foobar' datetime='datetime.datetime...)' data=u'foo' widget=u'rst' css_id=u''> So my question are : did I miss something ? Is this a bug ? Does on of you has a clue on how to fix this ? Btw, I use sqlobject 0.7.1 cheers, Luc |
From: Oleg B. <ph...@ph...> - 2008-07-17 09:41:06
|
On Thu, Jul 17, 2008 at 11:31:22AM +0200, Luc Milland wrote: > So my question are : did I miss something ? Is this a bug ? Does on of you has > a clue on how to fix this ? It is a bug in your application. ForeignKey and *Joins are intended to accept only integers. That ForeignKey and Joins are too forgiving and allow one to pass strings is a very small misfeature, but if you insist I'll add validators to protect the columns from strings. Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |
From: Luc M. <lu...@pt...> - 2008-07-17 10:02:23
|
thanks for fast answer ! > It is a bug in your application. Indeed, I missed something : turbogears provides a @validate() decorator which relay on module FormEncode and converts the string values to the appropriate python types. This should do the trick. cheers, Luc |