{"id":6592,"date":"2020-04-16T11:43:48","date_gmt":"2020-04-16T11:43:48","guid":{"rendered":"https:\/\/www.garysieling.com\/blog\/?p=6592"},"modified":"2020-04-16T11:43:48","modified_gmt":"2020-04-16T11:43:48","slug":"postgres-sql-error-except-types-text-and-json-cannot-be-matched","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/postgres-sql-error-except-types-text-and-json-cannot-be-matched\/","title":{"rendered":"Postgres SQL ERROR: EXCEPT types text and json cannot be matched"},"content":{"rendered":"\n<p>Say you&#8217;re using the excellent Postgres JSON features.  You may find that you get errors writing queries that use two tables &#8211; I often like to use &#8220;except&#8221; to diff datasets.<\/p>\n\n\n\n<p>If you write a SQL query like the following, you can get an error:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">with pre as (\n  select a, b from my_table\n), post as (\n  select re->'a', re->'b' from log\n)\nselect * from pre\n\nexcept\n\nselect * from post\n<\/pre>\n\n\n\n<p>While this looks like what you might want (the -> syntax looks intuitively correct), it uses the wrong operator, and you&#8217;ll get a type error:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>EXCEPT types text and json cannot be matched<\/p><\/blockquote>\n\n\n\n<p>The answer is to use the -&gt;&gt; operator rather than -&gt;. The &#8220;-&gt;&#8221; is equivalent to lodash _.pick, whereas -&gt;&gt; is like doing object[key].<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fixing a SQL error with Postgres JSON<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[322,437,523],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/6592"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=6592"}],"version-history":[{"count":3,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/6592\/revisions"}],"predecessor-version":[{"id":6595,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/6592\/revisions\/6595"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=6592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=6592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=6592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}