Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support NULL values for hstore keys #20

Open
acannistra opened this issue Aug 26, 2021 · 0 comments
Open

Support NULL values for hstore keys #20

acannistra opened this issue Aug 26, 2021 · 0 comments
Assignees

Comments

@acannistra
Copy link
Contributor

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 {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant