Autocomplete en Illegal mix of collations exception caused by Drupal 8 Autocomplete with Entity Reference View <span>Illegal mix of collations exception caused by Drupal 8 Autocomplete with Entity Reference View</span> <span><span lang="" about="/en/user/8" typeof="schema:Person" property="schema:name" datatype="">patrick</span></span> <span><time datetime="2017-09-08T11:48:14+02:00" title="Friday, September 8, 2017 - 11:48">Fri, 09/08/2017 - 11:48</time> </span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><p>Looking at a <em>Recent log messages</em> report of one of our Drupal 8 instaces, I came across this error message:</p> <blockquote> <p><em>Drupal\Core\Database\DatabaseExceptionWrapper</em>: Exception in Media Autocomplete[media_autocomplete]: SQLSTATE[HY000]: General error: 1267 Illegal mix of collations (ascii_general_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation 'like': SELECT media_field_data.langcode AS media_field_data_langcode, media_field_data.mid AS mid, AS media_field_data_name, media_field_data.bundle AS media_field_data_bundle FROM {media_field_data} media_field_data WHERE ((media_field_data.bundle = :db_condition_placeholder_0) AND (( LIKE :db_condition_placeholder_1 ESCAPE '\\') OR (media_field_data.bundle LIKE :db_condition_placeholder_2 ESCAPE '\\'))) AND ((media_field_data.status = :db_condition_placeholder_3) AND (media_field_data.langcode IN (:db_condition_placeholder_4, :db_condition_placeholder_5, :db_condition_placeholder_6))) LIMIT 10 OFFSET 0; Array ( [:db_condition_placeholder_0] =&gt; image [:db_condition_placeholder_1] =&gt; %gläsernes labor% [:db_condition_placeholder_2] =&gt; %gläsernes labor% [:db_condition_placeholder_3] =&gt; 1 [:db_condition_placeholder_4] =&gt; de [:db_condition_placeholder_5] =&gt; und [:db_condition_placeholder_6] =&gt; zxx ) in <em>Drupal\views\Plugin\views\query\Sql-&gt;execute()</em> (line <em>1488</em> of <em>core/modules/views/src/Plugin/views/query/Sql.php</em>).</p> </blockquote> <p>Clearly, an editor was trying to use the autocomplete functionality of an entity reference field searching for media entities matching the search string "gläsernes labor" (in English: <em>transparent laboratory</em>). This would actually happen for any search string entered into the autocomplete that didn't contain ascii characters only, so in this case the "ä" in "gläsernes" was causing the exception. Looking closer at the query, we can see that the MySQL table is "media_field_data" and the search string is in two placeholders: "db_condition_placeholder_1" and "db_condition_placeholder_2". These are used to check for matches in "" or "media_field_data.bundle" columns. So I asked the database for the collation of these fields:</p> <div class="geshifilter"><pre class="php geshifilter-php" style="font-family:monospace;">MariaDB <span style="color: #009900;">&#91;</span>my_drupal_8_database<span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span> SELECT TABLE_NAME<span style="color: #339933;">,</span> COLUMN_NAME<span style="color: #339933;">,</span> COLLATION_NAME FROM INFORMATION_SCHEMA<span style="color: #339933;">.</span>COLUMNS WHERE TABLE_SCHEMA<span style="color: #339933;">=</span><span style="color: #0000ff;">'my_drupal_8_database'</span> AND TABLE_NAME<span style="color: #339933;">=</span><span style="color: #0000ff;">'media_field_data'</span> AND COLUMN_NAME IN <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'bundle'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #339933;">+------------------+-------------+--------------------+</span> <span style="color: #339933;">|</span> TABLE_NAME <span style="color: #339933;">|</span> COLUMN_NAME <span style="color: #339933;">|</span> COLLATION_NAME <span style="color: #339933;">|</span> <span style="color: #339933;">+------------------+-------------+--------------------+</span> <span style="color: #339933;">|</span> media_field_data <span style="color: #339933;">|</span> bundle <span style="color: #339933;">|</span> ascii_general_ci <span style="color: #339933;">|</span> <span style="color: #339933;">|</span> media_field_data <span style="color: #339933;">|</span> name <span style="color: #339933;">|</span> utf8mb4_general_ci <span style="color: #339933;">|</span> <span style="color: #339933;">+------------------+-------------+--------------------+</span></pre></div> <p>Tada! The bundle column of the table uses ascii collation. For our autocomplete search to work, those two columns would need to have the same collation or we would need to change the query to specify the collation to use for each like condition separately. Another option would be to just query the name field or the bundle field.</p> <p>But why was the bundle field searched in the first place? We set up an entity reference view for this entity reference field (after applying a <a href="">patch</a>, mind you ;). We did this because we wanted to show the bundle and some other stuff along the label of the entity in the results returned by the autocomplete. When we set up the view we selected "Name" and "Bundle" for the "Search fields" setting of the "Entity reference list" style plugin.</p> <p>Our client didn't really care that much about searching the bundle field, so I just removed it from the search fields. And voilá, you could enter strings with umlauts into the autocomplete again.</p></div> <div class="field field--name-field-systems field--type-entity-reference field--label-inline"> <div class="field--label">Systems</div> <a href="/de/systems/drupal-8-9" class="label label-default" hreflang="de">Drupal 8/9</a> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline"> <div class="field--label">Tags</div> <a href="/en/tags/autocomplete" class="label label-default" hreflang="en">Autocomplete</a> <a href="/de/tags/views" class="label label-default" hreflang="de">Views</a> <a href="/en/tags/entity-reference-field" class="label label-default" hreflang="en">Entity Reference Field</a> <a href="/de/tags/mysql" class="label label-default" hreflang="de">mysql</a> <a href="/en/tags/exception" class="label label-default" hreflang="en">Exception</a> </div> Fri, 08 Sep 2017 09:48:14 +0000 patrick 127 at