[storm] None vs. undefined
Gustavo Niemeyer
gustavo at niemeyer.net
Wed Oct 3 15:54:47 BST 2007
Hello Stormers,
The explanation posted on the bug Carsten Grohmann opened
in Launchpad (#148553) might be interesting for more people,
so I'm copying it over here for reference.
-----
Hello Cartsten,
I got through your example and figured out what's going on. Here's the
actual issue:
class Share(object):
__storm_table__ = "t_share"
id = Int(primary=True)
(...)
def __init__(self, **kwargs):
self.id = kwargs.get('id')
When you are creating an object, there's a difference between assigning
None to an attribute and leaving it unassigned. When you assign None,
Storm will interpret that as NULLing out that value, and will send that
column to the database as such. When nothing is assigned to an attribute
(or the attribute is deleted with something like "del obj.attr"), Storm
will interpret that as the value being undefined, and will ask the
database for that value in the next opportunity.
In the case above, self.id is being assigned the value None explicitly,
and so this is sent to the database as a NULL. Coincidently, sqlite3
handles NULLs in integer primary keys as "do the autoincrement", and so
the database is changing the value behind Storm's back, making it
effectively "lose the object" (that's what the LostObjectError is
about).
If you make the following small change on that code, it will work:
if "id" in kwargs:
self.id = kwargs["id"]
For all other cases, it will work fine to send the NULL values. The only
disadvantage of doing so is that if the database has columns with
default values, Storm won't retrieve these, since it's setting these
values to NULL.
Thanks for providing a self-contained example. It's a lot easier to
discuss issues when there's an example like that.
--
Gustavo Niemeyer
http://niemeyer.net
More information about the storm
mailing list