Views adding wrong conditions in JOIN statements; returns zero results

I have a view (in Drupal 8.9) that’s not really all that complicated, but it does include relationships, filters, and a contextual filter. Some of the conditions added by views to the LEFT JOIN clauses are wrong, and cause the view to return zero results.

Here’s what it is supposed to do:

  • Filter: content type = "Supplier"
  • Relationship: link content from field_supplier in "Container" content type
  • Relationship: link content from field_customer in "Container" content type
  • Contextual Filter: match logged-in user to field_users in "Customer" content type

Essentially, that should return all suppliers a customer (based on logged-in user) has purchased containers from.

Here is the generated query:

SELECT node_field_data.nid AS nid, node_field_data_node__field_supplier.nid AS node_field_data_node__field_supplier_nid, node_field_data_node__field_customer.nid AS node_field_data_node__field_customer_nid FROM {node_field_data} node_field_data  LEFT JOIN {node__field_supplier} node__field_supplier    ON node_field_data.nid = node__field_supplier.entity_id   AND node__field_supplier.deleted = '0'   AND (node__field_supplier.langcode = node_field_data.langcode        OR node__field_supplier.bundle = 'purchase_contract')  LEFT JOIN {node_field_data} node_field_data_node__field_supplier    ON node__field_supplier.field_supplier_target_id = node_field_data_node__field_supplier.nid  LEFT JOIN {node__field_customer} node_field_data_node__field_supplier__node__field_customer    ON node_field_data_node__field_supplier.nid = node_field_data_node__field_supplier__node__field_customer.entity_id   AND node_field_data_node__field_supplier__node__field_customer.deleted = '0'   AND (node_field_data_node__field_supplier__node__field_customer.langcode = node_field_data_node__field_supplier.langcode        OR node_field_data_node__field_supplier__node__field_customer.bundle = 'sales_contract')  LEFT JOIN {node_field_data} node_field_data_node__field_customer    ON node_field_data_node__field_supplier__node__field_customer.field_customer_target_id = node_field_data_node__field_customer.nid  LEFT JOIN {node__field_users} node_field_data_node__field_customer__node__field_users    ON node_field_data_node__field_customer.nid = node_field_data_node__field_customer__node__field_users.entity_id   AND node_field_data_node__field_customer__node__field_users.deleted = '0'   AND (node_field_data_node__field_customer__node__field_users.langcode = node_field_data_node__field_customer.langcode        OR node_field_data_node__field_customer__node__field_users.bundle = 'supplier')  WHERE ((node_field_data_node__field_customer__node__field_users.field_users_target_id = '95')) AND (node_field_data.type IN ('supplier')) ORDER BY node_field_data.title ASC 

There are three parts of that query that are similar, so I’ll just point out one of them. In the last LEFT JOIN, just above the WHERE clause, is this:

AND (node_field_data_node__field_customer__node__field_users.langcode = node_field_data_node__field_customer.langcode        OR node_field_data_node__field_customer__node__field_users.bundle = 'supplier') 

First, I don’t understand why this is using an "OR". Either user’s langcode = customer field’s langcode –OR– bundle of customer field is "supplier"? That doesn’t make sense. That field_users exists on two different content types – Supplier and Customer. In this view, the contextual filter is supposed to be on users attached to Customer nodes, so that is just plain wrong.

The other two parts that are similar are where it checks node__field_supplier.bundle = 'purchase_contract' and node_field_data_node__field_supplier__node__field_customer.bundle = 'sales_contract'

Those are all things views adds to the query sort of "behind the scenes". I don’t see how I can configure my view any differently to affect those parts of the SQL.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Views adding wrong conditions in JOIN statements; returns zero results

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.