{"id":36029,"date":"2024-07-28T11:41:06","date_gmt":"2024-07-28T01:41:06","guid":{"rendered":"https:\/\/database.guide\/?p=36029"},"modified":"2024-07-28T11:41:08","modified_gmt":"2024-07-28T01:41:08","slug":"so-postgresql-array_append-works-but-array_prepend-doesnt-try-this","status":"publish","type":"post","link":"https:\/\/database.guide\/so-postgresql-array_append-works-but-array_prepend-doesnt-try-this\/","title":{"rendered":"So PostgreSQL ARRAY_APPEND() Works but ARRAY_PREPEND() Doesn&#8217;t? Try this."},"content":{"rendered":"\n<p class=\"\">If you&#8217;re updating arrays in PostgreSQL and you&#8217;ve suddenly realised that some of the arrays aren&#8217;t being updated, it could be due to the following.<\/p>\n\n\n\n<p class=\"\">If you&#8217;ve been using the <code>array_append()<\/code> function and the <code>array_prepend()<\/code> function, you may have found that one function works but the other doesn&#8217;t. For example <code>array_append()<\/code> works but <code>array_prepend()<\/code> doesn&#8217;t, or vice-versa.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"\">This could be due to a little &#8220;gotcha&#8221; with regards to the syntax of these two functions. Basically these two functions accept their arguments in the opposite order to each other. So if you&#8217;re switching between the two functions, you&#8217;ll need to switch your arguments too.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example of the Problem<\/h2>\n\n\n\n<p class=\"\">Here&#8217;s a simple example that demonstrates the issue.<\/p>\n\n\n\n<p class=\"\">Suppose we run the following statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT array_append(ARRAY&#91; 'Cat', 'Dog' ], 'Rabbit');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{Cat,Dog,Rabbit}<\/pre>\n\n\n\n<p class=\"\">That worked fine. We used <code>array_append()<\/code> to append <code>3<\/code> to the array.<\/p>\n\n\n\n<p class=\"\">But now let&#8217;s change <code>array_append()<\/code> to <code>array_prepend()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT array_prepend(ARRAY&#91; 'Cat', 'Dog' ], 'Rabbit');<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ERROR:  could not find array type for data type text[]<\/pre>\n\n\n\n<p class=\"\">We get an error.<\/p>\n\n\n\n<p class=\"\">The error message may differ, depending on the data in the array. For example, here&#8217;s another error message:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT array_prepend(ARRAY&#91; 1, 2 ], 0);<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ERROR:  function array_prepend(integer[], integer) does not exist<br>LINE 1: SELECT array_prepend(ARRAY[ 1, 2 ], 0);<br>               ^<br>HINT:  No function matches the given name and argument types. You might need to add explicit type casts.<\/pre>\n\n\n\n<p class=\"\">Either way, it&#8217;s the same issue &#8211; we&#8217;re using the wrong syntax for the <code>array_prepend()<\/code> function. Our arguments are in the wrong order.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Solution<\/h2>\n\n\n\n<p class=\"\">To fix this problem, all we need to do is swap the arguments around. So that the new value is the first argument:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT array_prepend('Rabbit', ARRAY&#91; 'Cat', 'Dog' ]);<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{Rabbit,Cat,Dog}<\/pre>\n\n\n\n<p class=\"\">Same with the other example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT array_prepend(0, ARRAY&#91; 1, 2 ]);<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{0,1,2}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re updating arrays in PostgreSQL and you&#8217;ve suddenly realised that some of the arrays aren&#8217;t being updated, it could be due to the following. If you&#8217;ve been using the array_append() function and the array_prepend() function, you may have found that one function works but the other doesn&#8217;t. For example array_append() works but array_prepend() doesn&#8217;t, &#8230; <a title=\"So PostgreSQL ARRAY_APPEND() Works but ARRAY_PREPEND() Doesn&#8217;t? Try this.\" class=\"read-more\" href=\"https:\/\/database.guide\/so-postgresql-array_append-works-but-array_prepend-doesnt-try-this\/\" aria-label=\"Read more about So PostgreSQL ARRAY_APPEND() Works but ARRAY_PREPEND() Doesn&#8217;t? Try this.\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[169,10],"class_list":["post-36029","post","type-post","status-publish","format-standard","hentry","category-postgresql","tag-arrays","tag-how-to"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36029","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=36029"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36029\/revisions"}],"predecessor-version":[{"id":36639,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/36029\/revisions\/36639"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=36029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=36029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=36029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}