Postgresql/psycopg2 does not like str enum values

Bug #927433 reported by brane
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Storm
New
Undecided
Unassigned

Bug Description

Tested with:
  * postgresql: 8.3, 9.0
  * Storm: 0.17, 0.18, 0.19
  * psycopg2: 2.2, 2.4.4

As the attached test case shows, if you assign a plain str value to an Enum() property, that value gets sent to the database as bytea, which Postgres does not like to convert to ENUM values, and Storm throws the following error:

    ProgrammingError: column "ident" is of type enumtype but expression is of type bytea

I've been using attached patch to EnumVariable.parse_get for several years and haven't yet had problems with it.

Revision history for this message
brane (brane-apache) wrote :
Revision history for this message
brane (brane-apache) wrote :
Revision history for this message
brane (brane-apache) wrote :
Revision history for this message
James Henstridge (jamesh) wrote :

Storm uses byte strings to represent binary data and unicode strings to represent text. Enumeration labels are text, so if you are using byte strings to represent them in your code that sounds like a misuse of the API.

Unless I'm misunderstanding what you're trying to do, this bug is probably invalid.

Revision history for this message
brane (brane-apache) wrote :

I'm assuming that if I'm allowed to define

class E(Storm):
   x = Enum(map={'A':'A'})

then it follows that I'm also allowed to assign

   E().x = 'A'

and expect the flush to work. If that's not the case, Enum should be coercing the labels to Unicode, and EnumVariable should be checking the values; like, e.g.,. UnicodeVariable does.

Revision history for this message
James Henstridge (jamesh) wrote :

You should be using a unicode string for the values in the map dictionary. Something like:

    x = Enum(map={'A': u'A'})

As I said before, the values you want sent to the database are text rather than binary data. Therefore, you need to use a unicode string.

Revision history for this message
brane (brane-apache) wrote :

Looks like your suggestion works, up to a point. In the course of verifying it, I ran into another bug that indicates that the EnumVariable implementation is flakey in other ways. Unless I'm completely misunderstanding the intent of _get_map and _set_map, of course. See the attached updated test case, and I'll submit a separate bug report about that.

Revision history for this message
brane (brane-apache) wrote :

Meh, belay that ... silly bug in the test script. Here's a better one.

This example highlights another bug related to enums and unicode, this time apparently in the expression compiler. I'm at a loss to understand how I can get this error:

ProgrammingError: operator does not exist: enumtype = bytea
LINE 1: DELETE FROM stormenum WHERE stormenum.ident = E'\\x42'::byte...

if both the keys and the values in the map are unicode.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.