Hi there,
I seem to be getting an incorrect query being built when I have node access enabled on the content types implementing node references. Here's the query:
PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_data_field_collection0.nid' in 'on clause': SELECT DISTINCT field_data_field_collection0.entity_type AS entity_type, field_data_field_collection0.entity_id AS entity_id, field_data_field_collection0.revision_id AS revision_id, field_data_field_collection0.bundle AS bundle FROM {field_data_field_collection} field_data_field_collection0 INNER JOIN {field_data_field_media_type} field_data_field_media_type1 ON field_data_field_media_type1.entity_type = field_data_field_collection0.entity_type AND field_data_field_media_type1.entity_id = field_data_field_collection0.entity_id INNER JOIN {node} node ON node.nid = field_data_field_collection0.entity_id LEFT OUTER JOIN {node_access} na ON na.nid = field_data_field_collection0.entity_id INNER JOIN {node_access} na_2 ON na_2.nid = field_data_field_collection0.nid WHERE (field_data_field_collection0.field_collection_nid = :db_condition_placeholder_0) AND (field_data_field_media_type1.field_media_type_tid = :db_condition_placeholder_1) AND (field_data_field_collection0.deleted = :db_condition_placeholder_2) AND (node.type = :db_condition_placeholder_3) AND (field_data_field_collection0.entity_type = :db_condition_placeholder_4) AND(((( (na.gid = :db_condition_placeholder_5) AND (na.realm = :db_condition_placeholder_6) )OR( (na.gid = :db_condition_placeholder_7) AND (na.realm = :db_condition_placeholder_8) ))AND (na.grant_view >= :db_condition_placeholder_9) AND (field_data_field_collection0.entity_type = :db_condition_placeholder_10) )OR (field_data_field_collection0.entity_type <> :db_condition_placeholder_11) )AND(( (na_2.gid = :db_condition_placeholder_12) AND (na_2.realm = :db_condition_placeholder_13) )OR( (na_2.gid = :db_condition_placeholder_14) AND (na_2.realm = :db_condition_placeholder_15) ))AND (na_2.grant_view >= :db_condition_placeholder_16) ; Array ( [:db_condition_placeholder_0] => 58 [:db_condition_placeholder_1] => 723 [:db_condition_placeholder_2] => 0 [:db_condition_placeholder_3] => media_file [:db_condition_placeholder_4] => node [:db_condition_placeholder_5] => 0 [:db_condition_placeholder_6] => all [:db_condition_placeholder_7] => 1 [:db_condition_placeholder_8] => gle [:db_condition_placeholder_9] => 1 [:db_condition_placeholder_10] => node [:db_condition_placeholder_11] => node [:db_condition_placeholder_12] => 0 [:db_condition_placeholder_13] => all [:db_condition_placeholder_14] => 1 [:db_condition_placeholder_15] => gle [:db_condition_placeholder_16] => 1 ) in field_sql_storage_field_storage_query() (line 577 of /var/www/clientname/www/modules/field/modules/field_sql_storage/field_sql_storage.module).
The problem seems to be that node_query_node_access_alter() (or rather _node_query_node_access_alter()) is assuming that the join for any node access query is going to connect to a field_data_field_name.nid, whereas in fact the correct column using node reference is field_data_field_name.field_name_nid - resulting in a PDOException.
I'm still working out a solution to this, or whether this should really be elevated to a bug with core, but if anyone has an immediate solution then I'm all ears.
Thanks,
Chris
Comments
Comment #1
xorne commentedHi,
Same issue for me with the latest version of views, and it's a problem with this reference module, even with the dev version.
Any idea how to fix that?
Thanks
Comment #2
xorne commentedOk it's fixed with the dev version but you need to clear all cache :P
Comment #3
DeFr commentedDuplicate of #1275096: Exception: SQLSTATE[42S22]: Column not found: 1054 Unknown column ... , fixed in dev version.
Comment #4
Anonymous (not verified) commentedI thought that this issue had actually been resolved by the above patch, but upon further inspection the problem does seem to bounce back to core, rather than references itself.
My node reference is being used in an EFQ rather than a view, and #1275096: Exception: SQLSTATE[42S22]: Column not found: 1054 Unknown column ... seems to only apply to Views relationships. I think this may actually be related to #997394: field_sql_query hardcodes a 'node_access' tag on EFQs with a fieldCondition.
Trying to figure out a solution.
Comment #5
xjmPostponing for details on what (if any) the core bug is.
Comment #6
Anonymous (not verified) commentedMy understanding of the issue (which may be incorrect) is that when a reference is made to a field using node access, _node_query_node_access_alter() checks whether it's linking to a node or to a field value - in this case, a field value which is in essence a foreign key to the node table (field_collection_nid on field_data_field_collection), the node reference.
The problem is that instead it's linking to a nid field on the field_data_field_collection table:
INNER JOIN {node_access} na_2 ON na_2.nid = field_data_field_collection0.nidFrom here, the node reference is already correctly joined to the parent table in order to gather the nodes to be returned as results, but there is an issue where the node_access table is joined on the value from the node reference field table - the join can't just be on the entity_id in that field as that's for the result nodes themselves (and that part works correctly through a LEFT OUTER JOIN), the joined reference node also needs to be checked, whether through an additional join to the node table which is then referenced by node_access, or through the ability to link to a foreign key field on a field table created by Field API.
If this is already possible and my understanding of how it's creating the base table relationship is wrong, I apologise - this may still be a problem with the References module itself and it may need to rewrite the query or its schema somehow to join in the correct way. I'll be happy to take my problem back there if that's what's needed.
Comment #8
bart vanhoutte commentedJust ran into the error using Drupal 8.1.3; Entity Reference Revisions 8.x-1.0-rc7 & Paragraphs 8.x-1.0-rc5. Using hook_node_access_records & hook_node_grants.
Comment #12
webcultist commentedI seem to have a similar problem. I created a paragraphs field. The paragraph has 2 fields, a normal text field and an entity reference field for taxonomy terms. I got an error when creating the fields and get an error when saving the node with the filled paragraph:
Comment #14
szato commentedI think we can close this issue, because of duplicate:
https://www.drupal.org/project/drupal/issues/2823957