Consider the following dynamic query:

$query = db_select('users', 'u');
$query->addField('u', 'name');
$query->distinct();
$query->join('node_revision', 'n');
$results = $query->execute();

In Drupal 7.9 this produces the following SQL:

SELECT DISTINCT u.name AS name
FROM users u
INNER JOIN node_revision n

Since no join condition is specified, this is a natural join, that is, an inner join on whatever columns in the tables have the same name. In this case that would be equivalent to

WHERE u.uid = n.uid

Some people frown on natural joins because they are a potential software maintenance headache; adding a column to one table which has the same name as a column in the other table changes the effective WHERE condition and therefore the results of the query, without generating any kind of warning. Other people use natural joins because they are convenient. The Drupal SQL coding conventions are silent on this subject.

The natural join SQL produces the expected result on MySQL, but produces a syntax error on PostgresSQL. The reason is that PostgresSQL requires the following:

SELECT DISTINCT u.name AS name
FROM users u
NATURAL JOIN node_revision n

to perform a natural join.

The database tests in modules/simpletest/tests do not try any natural joins, so this problem is not detected by the unit tests. However, natural joins appear in certain contributed modules, causing them to break when using PostgresSQL.

Comments

steinmb’s picture

Version: 7.9 » 7.x-dev
Component: database system » postgresql db driver
bzrudi71’s picture

Title: PostgresSQL syntax error from natural join » PostgreSQL: Add support for natural joins
Version: 7.x-dev » 8.0.x-dev
Issue tags: +PostgreSQL
Parent issue: » #2157455: [Meta] Make Drupal 8 work with PostgreSQL or remove support from core before release

I just stumbled upon this issue. Adding tags and parent issue. Also going to add this to the nice to have section in the parent issue.

Crell’s picture

I have vague memories of natural joins not working right in MySQL when we first wrote the DB layer (circa 2009), which is why we avoided them. If they're reliable in modern MySQL versions I've no problem with adding support for them. Most likely it would be an addition to the SelectQuery class/interface of the form

$query = $db->select('table1');
$query->naturalJoin('table2');
...

And then whether it uses INNER JOIN or NATURAL JOIN is left up to the DB-specific compilation method. Shouldn't be too hard to do. The question is whether that's safe for 8.1, since it's modifying an interface even if it's in an easy to support way and there's only about 4 people that would be impacted by it.

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

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

daffie’s picture

Version: 8.1.x-dev » 8.3.x-dev
Component: postgresql db driver » database system
Category: Bug report » Feature request
Issue tags: +API addition, +Needs tests

This issue creates new functionality and that is allowed. You are not allowed to change the existing functionality, but adding new functionality is allowed. So adding the function naturalJoin() with the desired functionality is no problem.

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.

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.

RoSk0’s picture

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.

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.

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

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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.