Skip to content

Support NULL values for hstore keys #20

@acannistra

Description

@acannistra

Hstore columns that contain keys that map to NULL values cause the hstore parser to fail. The cause of the problem is that the string representation of the hstore that we're using does not place quotes (") around NULL values like it does with all other values (come to think of it, this might also happen with integer values.) The string parsing method we're using to parse keys and values out of the hstore string representation fails if there are NULL values without quotes, because the parser tries (and fails) to remove the quotes.

The problem code:

def hstore_as_dict(hstore_str):
"""
Converts a string representation of a Postgres hstore
into a Python dictionary. hstore strings should be
of the form "'key1'=>'value1', "key2"=>"value2", ..."
:param hstore_str: hstore string
:type hstore_str: str
:rtype: dict
"""
if len(hstore_str) > 0:
return dict(
map(
lambda x: map(lambda x: x.strip().replace('"', ""), x.split("=>")),
hstore_str.split('", '),
)
)
else:
return {}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions