Error "Column 'langcode' in field list is ambiguous" thrown due to TranslationLanguageRenderer not rendering a field from a relationship

Created on 5 March 2015, over 9 years ago
Updated 18 April 2024, 7 months ago

Problem/Motivation

Views has a system in order to render fields in different languages. Either it's determined from a langcode which is part of the result, or it's taken from the current content / interface language, or simply a hardcoded langcode.

While the basic case works fine, the rendering by langcode of the current row [which is the default in Views] does not support using a relationship.

Example:

  • Add a text field to the User entity, and edit a user account to populate it.
  • Create a node from this author.
  • Create a node view, displaying fields (not teasers)
  • Add the author as relationship
  • Add the text field from User to the display
  • Select the translation to be based upon the row language [default setting]

You'll end up in an exception similar to this [shown exception is from a Taxonomy relationship, but User exception is similar]:

Drupal\Core\Database\IntegrityConstraintViolationException: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'langcode' in field list is ambiguous: SELECT node_field_data.title AS node_field_data_title, node_field_data.nid AS nid, node_field_data.langcode AS node_field_data_langcode, taxonomy_term_field_data_node_field_data.name AS taxonomy_term_field_data_node_field_data_name, taxonomy_term_field_data_node_field_data.vid AS taxonomy_term_field_data_node_field_data_vid, taxonomy_term_field_data_node_field_data.tid AS taxonomy_term_field_data_node_field_data_tid, users_field_data_node_field_revision__users.uuid AS users_field_data_node_field_revision__users_uuid, taxonomy_term_field_data_node_field_data__taxonomy_term_data.uuid AS taxonomy_term_field_data_node_field_data__taxonomy_term_data, node_field_data.created AS node_field_data_created, users_field_data_node_field_revision.uid AS users_field_data_node_field_revision_uid, langcode AS langcode FROM {node_field_data} node_field_data LEFT JOIN (SELECT td.*, tn.nid AS nid FROM {taxonomy_term_field_data} td LEFT JOIN {taxonomy_index} tn ON tn.tid = td.tid WHERE (td.vid IN (:db_condition_placeholder_1)) ) taxonomy_term_field_data_node_field_data ON node_field_data.nid = taxonomy_term_field_data_node_field_data.nid INNER JOIN {node_field_revision} node_field_revision ON node_field_data.vid = node_field_revision.vid LEFT JOIN {users_field_data} users_field_data_node_field_revision ON node_field_revision.uid = users_field_data_node_field_revision.uid INNER JOIN {users} users_field_data_node_field_revision__users ON users_field_data_node_field_revision.uid = users_field_data_node_field_revision__users.uid INNER JOIN {taxonomy_term_data} taxonomy_term_field_data_node_field_data__taxonomy_term_data ON taxonomy_term_field_data_node_field_data.tid = taxonomy_term_field_data_node_field_data__taxonomy_term_data.tid WHERE (( (node_field_data.status = :db_condition_placeholder_0) )) ORDER BY node_field_data_created DESC LIMIT 10 OFFSET 0; Array ( [:db_condition_placeholder_0] => 1 [:db_condition_placeholder_1] => tags ) in Drupal\Core\Database\Connection->query() (line 581 of core/lib/Drupal/Core/Database/Connection.php).

Proposed resolution

The problem is in the following piece of code:

    $langcode_key = $this->entityType->getKey('langcode');
    foreach (array('data_table', 'revision_table', 'base_table') as $key) {
      if ($table = $this->entityType->get($key)) {
        $table_alias = $query->ensureTable($table);  // HERE
        $this->langcodeAlias = $query->addField($table_alias, $langcode_key);
        break;
      }
    }

This code is trying to add the right 'langcode' field to an entity table in the view, basically by guessing what table it needs to add it to. So, $table is found, in this example, to be {users}. By default views doesn't know how to join from {node} to {users}.
In order to be able to do that, it has to take into account the relationship. Also, the langcode field that is added may need to have a better alias.

The solution is to pass along the relationship and leverage it.

Remaining tasks

Steps to reproduce on a supported version of Drupal

User interface changes

API changes

πŸ› Bug report
Status

Closed: cannot reproduce

Version

11.0 πŸ”₯

Component
ViewsΒ  β†’

Last updated about 13 hours ago

Created by

πŸ‡©πŸ‡ͺGermany dawehner

Live updates comments and jobs are added and updated live.
  • VDC

    Related to the Views in Drupal Core initiative.

  • D8MI

    (Drupal 8 Multilingual Initiative) is the tag used by the multilingual initiative to mark core issues (and some contributed module issues). For versions other than Drupal 8, use the i18n (Internationalization) tag on issues which involve or affect multilingual / multinational support. That is preferred over Translation.

  • Triaged core major

    There is consensus among core maintainers that this is a major issue. Only core committers should add this tag.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    I tested this on Drupal 10.1.x, standard install. I tried to follow the steps the Issue Summary but I don't know what the last step means, "Select the translation to be based upon the row language [default setting]". I tried the steps in #4 and #18 and was not able to produce an error.

    In order to continue here we need complete steps to reproduce the issue β†’ (starting from "Install Drupal core"). I am setting the status to Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

  • Status changed to Closed: cannot reproduce 8 months ago
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Since we need more information to move forward with this issue,and it has not been supplied in the past two years, I am closing this issue.

    If you are experiencing this problem on a supported version of Drupal reopen the issue, by setting the status to 'Active', and provide complete steps to reproduce the issue β†’ (starting from "Install Drupal core").

    Thanks!

  • πŸ‡ΊπŸ‡ΈUnited States damondt

    Not reopening without being sure of all steps to reproduce, but I ran into this on Drupal 10 with a paragraph relationship and a rendering language of "Content Language of View Row"

Production build 0.71.5 2024