Updated: Comment #12

Problem/Motivation

I tried clicking on random things after running through the installation on Postgresql latest HEAD 2/10/2014. After I clicked Files tab under Content (admin/content/files), I found this error. It looks like there are multiple fid columns included in this query statement.

PDOException: SQLSTATE[42702]: Ambiguous column: 7 ERROR: column reference "fid" is ambiguous LINE 5: GROUP BY fid, file_managed_filename, file_managed_uri, file_... ^ in PDOStatement->execute() (line 61 of /Applications/MAMP/vhosts/drupal-git/core/lib/Drupal/Core/Database/Statement.php). 

This prevents an administration user from using the admin/content/files interface. I have not tried the File Usage view display, but that may be unusable as well.

@merlinofchaos mentioned in a commit in 2009 that the base field should not have an alias.

Proposed resolution

Add the table.field syntax to the base field when it is added in a group by. This works because the base field does not have an alias per @merlinofchaos' comments above. There should not be any case of other fields that are just base field as field handlers should ensureTable() when adding fields or where clauses.

Can be manually tested by going to File Administration on Drupal 8 installation with PostgreSQL test bot or other install.

Automated test have been added for the test bot as well in QueryGroupByTest class.

Remaining tasks

  • Review

User interface changes

None.

API changes

None.

Original report by mradcliffe

Comments

bzrudi71’s picture

Good catch! Yes, the error is coming from files view /admin/structure/views/view/files. Changing aggregation settings for fid from 'Group results together' to 'Standard deviation' fixes the issue. No idea if this will cause any side issues ;-)

thatoneguy’s picture

Removing File: File ID from the view and adding File Usage: Entity ID instead also resolves the error. I would not recommend switching to standard deviation, as the File ID is an identification number used to link the file usage count to another view, and not intended for us as a statistic. The link would need to be changed to reference the [id] token instead of [fid].

swentel’s picture

I can't reproduce this on MySQL with standard install, so there's at least no problem there. Is this still a problem with PostgreSQL ?

mradcliffe’s picture

Just happened to be working on another issue and saw this in my e-mail. I reproduced this issue again.

Full postgresql.log

2014-08-24 15:43:09 UTC ERROR:  column reference "fid" is ambiguous at character 587
2014-08-24 15:43:09 UTC STATEMENT:  SELECT file_managed.fid AS fid, file_managed.filename AS file_managed_filename, file_managed.uri AS file_managed_uri, file_managed.filemime AS file_managed_filemime, file_managed.filesize AS file_managed_filesize, file_managed.status AS file_managed_status, file_managed.created AS file_managed_created, file_managed.changed AS file_managed_changed, SUM(file_usage_file_managed.count) AS file_usage_file_managed_count, MIN(file_managed.fid) AS fid_1
	FROM 
	file_managed file_managed
	LEFT JOIN file_usage file_usage_file_managed ON file_managed.fid = file_usage_file_managed.fid
	GROUP BY fid, file_managed_filename, file_managed_uri, file_managed_filemime, file_managed_filesize, file_managed_status, file_managed_created, file_managed_changed
	ORDER BY file_managed_changed DESC
	LIMIT 51 OFFSET 0
mradcliffe’s picture

I think this is it. If it used the full table alias instead of the base table exception alias, then I think that would solve the issue.

Edit: yes, kind of.

mradcliffe’s picture

Component: file system » views.module

Back in 7.x-3.x initial commit, (2009-05-17) @merlinofchaos added a comment regarding that the base table base field should always just be the field alias and not the table alias and the field alias. I'm not sure why this is, but that's the bug because field_managed and field_usage have a common column name.

If I let views add the field alias normally, then it works, but I don't know what that would break.

  public function addField($table, $field, $alias = '', $params = array()) {                                                                                    
    // We check for this specifically because it gets a special alias.
    if ($table == $this->view->storage->get('base_table') && $field == $this->view->storage->get('base_field') && empty($alias)) {
      $alias = $this->view->storage->get('base_field');
    }  

Another test would be see if this is reproducible in postgresql on Drupal 7 views 3.

mradcliffe’s picture

Status: Active » Needs review
StatusFileSize
new683 bytes

Ok, I think this is the right way to do it if base field alias is important as @merlinofchaos mentioned.

mradcliffe’s picture

StatusFileSize
new786 bytes
new658 bytes

Added comments to explain code. I guess this may not be necessary.

The last submitted patch, 7: postgres-2192877-add-table-alias-7.patch, failed testing.

bzrudi71’s picture

Yup! Patch works as expected locally. Let's wait for mysql bot and see...

mradcliffe’s picture

Issue tags: +Random test failure

Migrate test failed on the first run, but not the second. No code changes in the patch (comments added only).

mradcliffe’s picture

Issue summary: View changes
bzrudi71’s picture

Seems like bot fluke. Just to make sure, I did a docker test run with mysql-5.5 and Drupal\migrate_drupal\Tests\d6\MigrateDrupal6Test passes!

mradcliffe’s picture

Issue summary: View changes

Updated issue summary with explanation about patch that doesn't fit as code comments.

Yay, I'm getting over my terrible cold.

bzrudi71’s picture

For the records, this issue breaks FileListingTest as discovered in #2356979: PostgreSQL: Fix tests in file test group

bzrudi71’s picture

Status: Needs review » Reviewed & tested by the community

Patch passes testbot and works manually as expected. It also fixes a test failure within the file test group, so we should move forward here. RTBC

catch’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +VDC

I'd prefer to see a unit test here if we can so that any regression is caught not only by the postgres bot.

mradcliffe’s picture

Issue tags: +Needs tests

The query method depends on a couple of Drupal container methods so I think this will probably have to be a functional test or mocking the container.

The query method on views query plugin returns a database query object. The group property is protected so I don't think we can assert that the keys have table.field format. Doing a __toString() is bad as we're trying to work on another issue to always quote fields/aliases, which means that __toString() will never be the same.

So we can't go that deep. Hopefully we can instantiate the views query plugin sql object with the file overview view and some display plugin. And then after calling query method, assert that $this->groupby has keys in table.field format. The groupby variable is public on that query object.

jhedstrom’s picture

Issue tags: -Needs tests
StatusFileSize
new1.16 KB
new1.93 KB

This adds a test.

The last submitted patch, 21: ambiguous-column-2192877-21-TEST-ONLY.patch, failed testing.

mradcliffe’s picture

Issue summary: View changes
StatusFileSize
new1.05 KB
+++ b/core/modules/views/src/Tests/QueryGroupByTest.php
@@ -189,4 +189,20 @@ public function testGroupByCountOnlyFilters() {
+    $view->displayHandlers->get('default')->options['fields']['name']['group_type'] = 'min';
+    unset($view->displayHandlers->get('default')->options['fields']['id']['group_type']);

This bit confused me until I had to look at the test view "test_group_by_count". There are roughly 20 occurrences of unset() being used like this throughout Views tests.

Works for me. Thanks for the patch.

I attached an interdiff with command: interdiff postgres-2192877-add-table-alias-8.patch ambiguous-column-2192877-21.patch > interdiff-8-21.txt

mradcliffe’s picture

Status: Needs review » Reviewed & tested by the community

Flip back to RTBC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 21: ambiguous-column-2192877-21.patch, failed testing.

The last submitted patch, 21: ambiguous-column-2192877-21-TEST-ONLY.patch, failed testing.

mradcliffe’s picture

Status: Needs work » Reviewed & tested by the community

Testing infrastructure memory issue broke HEAD. Confirmed passing tests.

bzrudi71’s picture

Patch adds test coverage and looks clean, so +1 for RTBC. Just wonder if we should have an issue title and summary update. The patch fixes more than just the file listing view (more info)

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

This issue addresses a major bug and is allowed per https://www.drupal.org/core/beta-changes. Committed db1b2d2 and pushed to 8.0.x. Thanks!

  • alexpott committed db1b2d2 on 8.0.x
    Issue #2192877 by mradcliffe, jhedstrom: File content list includes...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.