Created
July 24, 2013 19:10
-
-
Save jahio/6073520 to your computer and use it in GitHub Desktop.
Typecasting data coming out of hstore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
testing=# \d foo | |
Table "public.foo" | |
Column | Type | Modifiers | |
--------+--------+----------- | |
id | uuid | not null | |
stuff | hstore | | |
Indexes: | |
"foo_pkey" PRIMARY KEY, btree (id) | |
testing=# SELECT * FROM foo; | |
id | stuff | |
--------------------------------------+-------------- | |
c1e4833e-96e3-4774-b5bf-39da5c096457 | "foo"=>"bar" | |
(1 row) | |
testing=# SELECT CAST(stuff->'foo' AS VARCHAR) FROM foo WHERE stuff->'foo'='bar'; | |
varchar | |
--------- | |
bar | |
(1 row) | |
testing=# SELECT CAST(stuff->'foo' AS UUID) FROM foo WHERE stuff->'foo'='bar'; | |
ERROR: invalid input syntax for uuid: "bar" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The final SELECT here, converting to a UUID, is just to show that if you try to convert a value to a type that doesn't match, it won't do it. In this case "bar" is the totally wrong format for a UUID (by a huge margin).