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

Postponed on #2457999: Cannot use relationship for rendered entity on Views, which may fix this.

User interface changes

API changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

Status: Active » Needs review
FileSize
9.75 KB

So far this is a version which works, but it seems to be that the test changes I made kinda broke stuff.

jhodgdon’s picture

Issue summary: View changes

A few notes:

a) I originally came across this issue when doing manual testing in #2429447-50: Use data table as views base table, if available.

b) The exception in the issue summary is from joining on a taxonomy reference field. Any Relationship will do, but just noting that the specific query exception posted there is from a taxonomy join.

c) Currently to trigger this problem you need to add a field to the entity you are joining, such as adding a text field to Users, and joining on User, and then display that custom field you created.

d) And it is only a problem for field-based views.

I'll update the issue summary.

Status: Needs review » Needs work

The last submitted patch, 1: 2446681-1.patch, failed testing.

penyaskito’s picture

I couldn't reproduce:

1. Installed language, content_translation.
2. Enabled content translation for nodes and users.
3. Followed the issue summary "Example" instructions.
4. Created a user, filled the field.
5. Translated the user and the field.
6. Created a node with that user as author.
7. Translated it.
8. The view shows 4 rows (no exception): node en-user en, node es-user en, node en-user es, node es-user es.

jhodgdon’s picture

That is very interesting. I wonder how/when it would have been fixed? I was not able to make the problem unless I made a view with a relationship between Node and another entity, and there was a plain Text field on the second entity that was translated, and the view was field-based and displaying that field on the second entity. Can you verify that is what you tested?

plach’s picture

I think this might have been fixed by #2454145: Replace user_name handler with Field API formatter, which added a relationship parameter to the renderer ::query method:

http://cgit.drupalcode.org/drupal/tree/core/modules/views/src/Entity/Ren...

jhodgdon’s picture

Status: Needs work » Closed (cannot reproduce)

OK then, let's close this then. Thanks!

Gábor Hojtsy’s picture

Issue tags: -sprint

Yay!

agentrickard’s picture

Status: Closed (cannot reproduce) » Active

I just saw this pop-up again in #2726507: Integrity Constraint Violation Exception when adding another language. Could be an issue with revisions? Not sure.

agentrickard’s picture

In this case it appears as if the langcode field is added twice, once (correctly) by {node_revision} and once incorrectly by {users}.

Screenshot of views fields.

agentrickard’s picture

The issue may be that there are _two_ relationships in this view. Note the broken fieldAliases.

Broken fieldAlias assignments

agentrickard’s picture

Interesting. In my case, the View has 5 fields. 4 use a relationship. One does not. It's that last one that causes the error.

I can fix that in the View by adding the missing relationship, but this still seems like a bug.

agentrickard’s picture

Version: 8.0.x-dev » 8.2.x-dev

This never got bumped when versions changed.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

cilefen’s picture

Issue tags: +Triaged core major

@timplunkett, @alexpott, @dawehner, @xjm and I discussed this in a triage session at DrupalCon Dublin. This is an exception thrown in the UI with a few simple steps. We agree this is major priority.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Lendude’s picture

The fix for #2457999: Cannot use relationship for rendered entity on Views adds relationship handling to TranslationLanguageRenderer so would expect this to be fixed when that lands.

johnv’s picture

Title: TranslationLanguageRenderer doesn't support rendering a field from a relationship » Error "Column 'langcode' in field list is ambiguous" thrown due to TranslationLanguageRenderer not rendering a field from a relationship

The same error occurs in a far easier example then in OP, using a View of Content Revisions
- Add a Field of type "List (integer) (module: options)" to a content type. Add a few keys. You may or may not use 'key-value combinations.
- create some content
- Create a view of Content Revisions, add the new field, add an exposed filter for the field to the view.
- Show the view: all is fine:
- Apply the filter: the following error appears:

Drupal\Core\Database\IntegrityConstraintViolationException: SQLSTATE[23000]: Integrity constraint violation: 1052 
Column 'langcode' in field list is ambiguous: 
SELECT 
node_field_revision.vid AS node_field_revision_vid, 
node_field_revision.vid AS vid, 
langcode AS langcode 
FROM {node_field_revision} node_field_revision 
INNER JOIN {node_revision__field_listonetoten} node_revision__field_listonetoten 
ON node_field_revision.vid = node_revision__field_listonetoten.revision_id 
AND (node_revision__field_listonetoten.deleted = :views_join_condition_0 
AND node_revision__field_listonetoten.langcode = node_field_revision.langcode) 
WHERE ((node_revision__field_listonetoten.field_listonetoten_value = :node_revision__field_listonetoten_field_listonetoten_value)) 
AND (node_field_revision.status = :db_condition_placeholder_2) 
ORDER BY node_field_revision_vid DESC LIMIT 21 OFFSET 0; 
Array ( [:node_revision__field_listonetoten_field_listonetoten_value] => 7 [:db_condition_placeholder_2] => 1 [:views_join_condition_0] => 0 ) in Drupal\Core\Database\Connection->handleQueryException() (line 682 of core\lib\Drupal\Core\Database\Connection.php).

The patch in #1 (which is outdated, so applied by hand manually) does not solve the problem.

Sam152’s picture

I tested the patch mentioned in #17 and that didn't fix this issue for me when it was manifested in #2862041: Provide useful Views filters for Content Moderation State fields.

Sam152’s picture

Status: Active » Needs review
FileSize
9.53 KB

Here is a test that proves this still exists, inspired by #1 but using a new view with a revision base table.

Hopefully helps work towards a solution for this.

Status: Needs review » Needs work

The last submitted patch, 20: 2446681-20.patch, failed testing. View results

Sam152’s picture

Here is a solution which fixes the problem for me, but it doesn't feel right for some reason.

Sam152’s picture

Status: Needs review » Needs work
plach’s picture

Assigned: Unassigned » plach

Working a bit on this

plach’s picture

Issue tags: +WI critical
plach’s picture

Title: Error "Column 'langcode' in field list is ambiguous" thrown due to TranslationLanguageRenderer not rendering a field from a relationship » [PP-1] Error "Column 'langcode' in field list is ambiguous" thrown due to TranslationLanguageRenderer not rendering a field from a relationship
Assigned: plach » Unassigned
Status: Needs work » Postponed
Issue tags: -WI critical
FileSize
4.97 KB

After digging into this, I think the issue we are having in #2862041: Provide useful Views filters for Content Moderation State fields is not actually related to this one, it's more about properly supporting entity revision views at entity row renderer level. Therefore I'm going to reopen the issue that was marked as duplicate and move there.

Attaching an updated version of the tests in #1 (plus some clean-up) applying to 8.4.x. I suspect this may be actually fixed by #2457999: Cannot use relationship for rendered entity on Views or should at least be postponed to that.

plach’s picture

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

samuel.mortenson’s picture

Just tried the most recent patch in #2457999: Cannot use relationship for rendered entity on Views and it doesn't resolve this problem. #22 is a quick fix for now, for anyone landing on this issue.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

kostik1337’s picture

FileSize
910 bytes

Rerolled patch for 8.9.0 without tests, because I had errors

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Issue summary: View changes
Issue tags: +Bug Smash Initiative

Adding what this is postponed on to the issue summary.

danflanagan8’s picture

Title: [PP-1] Error "Column 'langcode' in field list is ambiguous" thrown due to TranslationLanguageRenderer not rendering a field from a relationship » Error "Column 'langcode' in field list is ambiguous" thrown due to TranslationLanguageRenderer not rendering a field from a relationship
Issue summary: View changes
Status: Postponed » Needs work
Issue tags: +Needs manual testing

The blocking issue has been fixed. I'll un-postpone this.

There is some suspicion that the blocking issue will have fixed this as well, so I'm going to tag for manual testing. Can this still be reproduced?

danflanagan8’s picture

Version: 9.5.x-dev » 10.1.x-dev

Updating target branch to reflect where the former blocker was committed. That's where manual testing should happen to see if we can still reproduce this bug.

quietone’s picture

Issue summary: View changes
Status: Needs work » Postponed (maintainer needs more info)
Issue tags: -Needs manual testing +Needs steps to reproduce

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!

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)

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!

damondt’s picture

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".

Possibly helpful context:
View is output in a block.
Avoiding this error by switching to a rendering language of entity default or applying a patch from here leads to warnings about fields on the referenced entity not existing.