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
| Comment | File | Size | Author |
|---|---|---|---|
| #23 | interdiff-8-21.txt | 1.05 KB | mradcliffe |
| #21 | ambiguous-column-2192877-21.patch | 1.93 KB | jhedstrom |
| #21 | ambiguous-column-2192877-21-TEST-ONLY.patch | 1.16 KB | jhedstrom |
Comments
Comment #1
bzrudi71 commentedGood 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 ;-)
Comment #2
thatoneguy commentedRemoving
File: File IDfrom the view and addingFile Usage: Entity IDinstead 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].Comment #3
swentel commentedI can't reproduce this on MySQL with standard install, so there's at least no problem there. Is this still a problem with PostgreSQL ?
Comment #4
mradcliffeJust happened to be working on another issue and saw this in my e-mail. I reproduced this issue again.
Full postgresql.log
Comment #5
mradcliffeI 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.
Comment #6
mradcliffeBack 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.
Another test would be see if this is reproducible in postgresql on Drupal 7 views 3.
Comment #7
mradcliffeOk, I think this is the right way to do it if base field alias is important as @merlinofchaos mentioned.
Comment #8
mradcliffeAdded comments to explain code. I guess this may not be necessary.
Comment #10
bzrudi71 commentedYup! Patch works as expected locally. Let's wait for mysql bot and see...
Comment #11
mradcliffeMigrate test failed on the first run, but not the second. No code changes in the patch (comments added only).
Comment #12
mradcliffeComment #13
bzrudi71 commentedSeems 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!
Comment #14
mradcliffeUpdated issue summary with explanation about patch that doesn't fit as code comments.
Yay, I'm getting over my terrible cold.
Comment #15
bzrudi71 commentedFor the records, this issue breaks FileListingTest as discovered in #2356979: PostgreSQL: Fix tests in file test group
Comment #18
bzrudi71 commentedPatch 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
Comment #19
catchI'd prefer to see a unit test here if we can so that any regression is caught not only by the postgres bot.
Comment #20
mradcliffeThe 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.
Comment #21
jhedstromThis adds a test.
Comment #23
mradcliffeThis 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.txtComment #24
mradcliffeFlip back to RTBC.
Comment #29
mradcliffeTesting infrastructure memory issue broke HEAD. Confirmed passing tests.
Comment #30
bzrudi71 commentedPatch 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)
Comment #31
alexpottThis 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!