[SQLObject] id, str(id) and duplicate objects in the cache
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
|
From: Matt G. <ma...@po...> - 2003-07-20 15:21:57
|
I just found an interesting problem due to programmer (i.e. me) error but it leaves the SQLObject caches damaged so I wondered if SQLObject should cope better. The problem is probably best demonstrated by (untested) example with a description of what happens inside SQLObject's caches: id = Obj.new(col1='a', col2='b') # added to cache with key == int(1) o = Obj(id) # found in cache with key == int(1) o = Obj(str(id)) # note the cast to a string # reads from database, adds to cache with key == str(1) # Eek, two objects now in cache! o.col1, o.col2 = o.col2, o.col1 # writes change to db, only object str(id) in cache matches o = Obj(str(id)) # found in cache with key == str(1) o = Obj(id) # found in cache with key == int(1) but bad data Lovely isn't it, and all because I stored the object's id in a hidden form field ;-). I won't tell you how long it took to work out what was happening! My app is working again now but should SQLObject have let this happen? The obvious thing to do is to force the id to int() inside the Obj constructor but that would break any attempt to allow alphanumeric ids. So, perhaps a better solution is to always use a string as the cache keys? There's a TestCase attached that I was using to help debug this. Some of it is already covered by existing tests but it could be useful anyway. Cheers, Matt -- Matt Goodall, Pollenation Internet Ltd w: http://www.pollenation.net e: ma...@po... |