[storm] Questions about inserts
James Henstridge
james at jamesh.id.au
Fri Jun 13 09:58:51 BST 2008
2008/6/13 David Koblas <koblas at extra.com>:
> I've got two issues, the first is that I have a column that is basically
> a random number.. But, I cant quite figure out how to get in inserted
> correctly, what I thought would work is something akin to the following
> code:
>
> inv = schema.Invite()
> inv.invite_id = SQL("MD5(UUID())")
> inv.account_id = acct.id
> inv.email = unicode(toaddr)
>
> store.add(inv)
>
> Of course, first I get an exception that 'invite_id' is not unicode
> (since it's defined as Unicode() column). Of course a quick cast to
> unicode(...) yeilds nothing but trouble. So, is there a way I can pass
> a SQL expression into an add(...) statement?
If you try to access "inv.invite_id" after adding it to the store,
there would be a query to INSERT the new row, and then another query
to find out what invite_id on the new row is equal to. The error is
probably occurring on this second step, if the database adapter
doesn't return the column value as a unicode string (as expected for a
Unicode() column).
The storm.databases.mysql code passes "use_unicode=True,
charset='utf8'" to the MySQLdb.connect() function, which should cause
text data types to be returned as unicode. If that isn't happening,
it'd be good to investigate why.
> I'm finding it a bit tidious to constantly be casting from str(...) to
> unicode(...) to keep the database happy. Is there really a best
> practice for how to have VARCHAR columns that are should be treated as
> RawStr() for purposes of development?
There is an AutoUnicode column type in storm.sqlobject, used for the
sqlobject compatibility layer (since sqlobject lets you use str and
unicode interchangeably for the most part). You might find it useful,
but it is generally a good idea to keep your text data in a known
encoding (which the unicode class does for you).
James.
More information about the storm
mailing list