Currently, the 'File Usage: Content' relationship produces the following query:

SELECT file_managed.filename AS file_managed_filename, file_managed.fid AS fid, file_managed.filesize AS file_managed_filesize, file_managed.uri AS file_managed_uri, node_file_usage.nid AS node_file_usage_nid, node_file_usage.title AS node_file_usage_title
FROM 
{file_managed} file_managed
LEFT JOIN {file_usage} file_usage ON file_managed.fid = file_usage.fid
LEFT JOIN {node} node_file_usage ON file_usage.id = node_file_usage.nid AND node_file_usage.type = 'node'
WHERE (( (node_file_usage.nid IS NOT NULL ) ))
LIMIT 10 OFFSET 0

which is returning 0 items due to the AND node_file_usage.type = 'node' piece.

The 'File Usage: User' relationship works correctly, so extrapolating that relationship definition leads to the fix. patch attached.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

WorldFallz’s picture

the fix produces the following corrected query:

SELECT file_managed.filename AS file_managed_filename, file_managed.fid AS fid, file_managed.filesize AS file_managed_filesize, file_managed.uri AS file_managed_uri, node_file_usage.nid AS node_file_usage_nid, node_file_usage.title AS node_file_usage_title
FROM 
{file_managed} file_managed
LEFT JOIN {file_usage} file_usage ON file_managed.fid = file_usage.fid
LEFT JOIN {node} node_file_usage ON file_usage.id = node_file_usage.nid AND file_usage.type = 'node'
WHERE (( (node_file_usage.nid IS NOT NULL ) ))
LIMIT 10 OFFSET 0
WorldFallz’s picture

actually, no. That then breaks my download_count module views. Rather this patch seems to work in all cases.

TahitiPetey’s picture

WorldFallz, your patch works. However it removes the check for file_usage.type = 'node' on the LEFT JOIN (at least for me). Not sure if this will cause any problems down the road (it had occurred to me that the check seems rather redundant and unnecessary in the first place, but I don't know Views well enough to say...).

thinkyhead’s picture

Title: File Usage: Content relationship broken » File Usage: Content relationship broken
FileSize
439 bytes

I see in the latest 7.x-3.x dev that the offending 'extra' entry has simply been removed. This appears to be an error. As near as I can tell it should just duplicate the 'file_to_user' version of the 'extra' key with 'node' instead of 'user'. I've just done this on my current client's site and it produces the correct SQL. My patch for the current 7.x-3.x branch is attached.

WorldFallz’s picture

the current views 7.x-3.x-dev remains as originally reported (with 'extra' => array(array('field' => 'type', 'value' => 'node')),.

And your patch is identical to the one I originally provided-- the question remains as described in #3. Would be nice to get some more eyes on this.

thinkyhead’s picture

Indeed, my patch makes the same correction as your original patch. My question is, if the file_to_node join is incorrect, wouldn't the file_to_user join also be wrong?

So, to follow up on the issue, what does the broken query look like for your download_count view, as you mentioned in #2? Maybe there's a more subtle solution for that.

WorldFallz’s picture

Thanks for following up thinkyhead.

The query I get with the patch original patch is:

SELECT download_count.dcid AS dcid, download_count.id AS download_count_id, download_count.type AS download_count_type, file_managed_download_count.fid AS file_managed_download_count_fid, file_managed_download_count.filename AS file_managed_download_count_filename, uri AS uri
FROM 
{download_count} download_count
LEFT JOIN {file_managed} file_managed_download_count ON download_count.fid = file_managed_download_count.fid
LEFT JOIN {file_usage} file_managed_download_count__file_usage ON file_managed_download_count.fid = file_managed_download_count__file_usage.fid
LEFT JOIN {node} node_file_usage ON file_managed_download_count__file_usage.id = node_file_usage.nid AND file_usage.type = 'node'

Which also results in an error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'file_usage.type' in 'on clause'

And my relationship definition is:

  // fid
  $data['download_count']['fid'] = array(
    'title' => t('File'),
    'help' => t('The file information of the downloaded file.'),
    'relationship' => array(
      'base' => 'file_managed',
      'base field' => 'fid',
      'handler' => 'views_handler_relationship',
      'label' => t('File'),
    ),
  );

I add that relationship, and then chain the 'file usage: content' relationship on top of that to get at the node title.

I've been banging my head against the wall trying to get this resolved for weeks, and no matter what I do it seems the only thing that works is #2.

thinkyhead’s picture

Well you can see the file_usage table is being JOINed with a name reassignment to file_managed_download_count__file_usage - and it's not that file_usage.type is itself problematic. Perhaps there's a more fundamental issue in the fact that Views isn't keeping track of the name reassignment.

See if there's some way to make that last JOIN look like this:

LEFT JOIN {node} node_file_usage ON file_managed_download_count__file_usage.id = node_file_usage.nid AND file_managed_download_count__file_usage.type = 'node'

grossmann’s picture

I had similar problems and read through this issue. The patch on #0 (issue summary) worked for me but I got confused with the other patches.
@thinkyhead: Your patch works on another relationship than the original one (see line numbers of you patch).

@@ -268,6 +268,7 @@ function system_views_data() {
       'base' => 'file_managed',
       'base field' => 'fid',
       'relationship field' => 'fid',
+      'extra' => array(array('table' => 'file_usage', 'field' => 'type', 'operator' => '=', 'value' => 'node')),
     ),
   );

This is on the ['node_to_file'] relationship not the ['file_to_node'] relationship which is targeted in #0.

@@ -253,7 +253,7 @@ function system_views_data() {
       'base' => 'node',
       'base field' => 'nid',
       'relationship field' => 'id',
-      'extra' => array(array('field' => 'type', 'value' => 'node')),
+      'extra' => array(array('table' => 'file_usage', 'field' => 'type', 'operator' => '=', 'value' => 'node')),
     ),
   );

I did not test if this fixes node_to_file relationship but the on in #0 works. Maybe this is why you think they removed it on the latest dev (see #7).
grossmann-mcs

Johnny vd Laar’s picture

FileSize
755 bytes

The bug should be fixed in a higher level I think. Attached is some code that tries to resolve the alias for the table instead of just dropping it.

WorldFallz’s picture

I still get blank node titles for the patch in #10 against a fresh checkout of d7 dev. No error though.

Attached is a simple views export for the view I use to test this.

alexjarvis’s picture

Applying the change in #4 to a Views 3.1 install's $data['file_usage']['file_to_node'] fix the problem for me.

bschilt’s picture

Patch in #0 worked for me as I was doing a file to node relationship.

Johnny vd Laar’s picture

The view I was testing with was a lot more complex. I had a view of nodes that had media items and the files had a taxonomy field and the taxonomy field had a link field which I wanted to see in the nodes. So perhaps my change only worked well in my particular edge case.

andrewbelcher’s picture

Just had a look at the repository log... Looks like in commit #289b6c97b722596f7bdd505e1e8bd15f3be32b07 the 'table' => 'file_usage' was removed (along with 'operator' => '='). It looks like there are a few different problems here:

1) The removal of the 'table' broke the relationship as it tried to join based on the wrong column. This is fixed by adding back in (as done in the original patch).
2) Simply putting it back in brings up a different issue where the table name rather than the alias is getting used when the relationship get's turned into SQL. This looks like it's fixed by the patch in comment #10... This is the issue that was breaking the download count and possibly other potential things.
3) Simply removing the check doesn't really solve the issue, it just creates a different one if you have files used by multiple entities which have the same id.

rlmumford is currently applying the two patches, testing and hopefully soon will post a patch which solves both issues!

rlmumford’s picture

So here's the patch

WorldFallz’s picture

awesome-- thanks so much for taking time to explain the details andrew. I feel like I'm stabbing in the dark when I try to trouble shoot views issues like this and that type of explanation is really instructive.

And thanks for rolling the patch rlmumford!

I'm testing it now, and it seems to be fixing the issues.

I still have one odd issue, though fixable with a filter, that doesn't make sense to me.

I get the following query:

SELECT file_managed_download_count.uri AS file_managed_download_count_uri, download_count.id AS download_count_id, download_count.type AS download_count_type, node_file_usage.title AS node_file_usage_title, node_file_usage.nid AS node_file_usage_nid
FROM 
{download_count} download_count
LEFT JOIN {file_managed} file_managed_download_count ON download_count.fid = file_managed_download_count.fid
LEFT JOIN {file_usage} file_managed_download_count__file_usage ON file_managed_download_count.fid = file_managed_download_count__file_usage.fid
LEFT JOIN {node} node_file_usage ON file_managed_download_count__file_usage.id = node_file_usage.nid AND file_managed_download_count__file_usage.type = 'node'
LIMIT 10 OFFSET 0

However, the result set is still including files attached to user entities in spite of the LEFT JOIN {node} node_file_usage ON file_managed_download_count__file_usage.id = node_file_usage.nid AND file_managed_download_count__file_usage.type = 'node'.

That's probably specific to the download_count module, though so I'm not sure it should hold up this patch. Hopefully some others will test it out and we can rtbc this puppy.

WorldFallz’s picture

Adding a (File) File Usage: Entity type (= node) filter produces the following query:

SELECT file_managed_download_count.uri AS file_managed_download_count_uri, download_count.id AS download_count_id, download_count.type AS download_count_type, node_file_usage.title AS node_file_usage_title, node_file_usage.nid AS node_file_usage_nid
FROM 
{download_count} download_count
LEFT JOIN {file_managed} file_managed_download_count ON download_count.fid = file_managed_download_count.fid
LEFT JOIN {file_usage} file_managed_download_count__file_usage ON file_managed_download_count.fid = file_managed_download_count__file_usage.fid
LEFT JOIN {node} node_file_usage ON file_managed_download_count__file_usage.id = node_file_usage.nid AND file_managed_download_count__file_usage.type = 'node'
WHERE (( (file_managed_download_count__file_usage.type LIKE 'node' ESCAPE '\\') ))
LIMIT 10 OFFSET 0

Which correctly returns only nodes. Still don't know why though.

rlmumford’s picture

Have you tried setting the required flag on the relationship?

WorldFallz’s picture

rlmumford ftw-- I didn't realize that flag was tied to switching to an inner join. brilliant, lol. testing with some more complex views, but so far this seems to be the fix.

WorldFallz’s picture

i've test this quite a bit with all sorts of complex views, and it seems to be the fix. It would be good if someone else on this thread also tested it so we can rtbc it.

Johnny vd Laar’s picture

Status: Needs review » Reviewed & tested by the community

the patch also works for me

dawehner’s picture

Status: Reviewed & tested by the community » Needs work
+            if($view_query->table_queue[$this->left_table]['table'] == $info['table']) {
+              $join_table = $view_query->table_queue[$this->left_table]['alias'] . '.';
+            }

This line would probably need some documentation, so readers can understand what this means.
Sure i know what this means, and it makes somehow sense, it would be cool to add a one line description + some more stuff above in the views_join documentation.

rlmumford’s picture

Here's the patch re-rolled with some (hopefully) helpful comments

rlmumford’s picture

rlmumford’s picture

Status: Needs work » Needs review
dawehner’s picture

Here is a rerole of the patch with a short docu improvement.

tim.plunkett’s picture

Triggering the testbot.

Status: Needs review » Needs work

The last submitted patch, views-file_to_node_relationship_fix-1409454-27.patch, failed testing.

rlmumford’s picture

Status: Needs work » Needs review
FileSize
2.8 KB

Rerolled against latest dev

WorldFallz’s picture

Status: Needs review » Reviewed & tested by the community

bot tests pass, patch applies cleanly, retested again with some complex views and everything is working. New comments look good also.

looks gtg.

BTMash’s picture

Funny, I was about to post the same patch (minus the commenting) as I ran into this issue. So its a +1 for me as well.

tim.plunkett’s picture

FileSize
3.24 KB

Leaving as RTBC, but I cleaned up the comments a little bit.

dawehner’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
3.19 KB

Here is a slightly easier patch with less possible fails.

WorldFallz’s picture

Status: Needs review » Reviewed & tested by the community

views still work and definitely easier read. looks good.

dawehner’s picture

Version: 7.x-3.x-dev » 6.x-3.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Thanks for all contributors.

Committed to 7.x-3.x

WorldFallz’s picture

awesome... thanks everyone!

Chris Matthews’s picture

Issue summary: View changes
Status: Patch (to be ported) » Closed (outdated)

The Drupal 6 branch is no longer supported, please check with the D6LTS project if you need further support. For more information as to why this issue was closed, please see issue #3030347: Plan to clean process issue queue