I'm coming from issue #2087849 (for the view_unpublished) module. I have read #1413108: entityFieldQueryAlter() should check if correct table exists which unfortunately doesn't solve my problem. However, the route cause is the same: The field table is used, not the node table, and hence a column (nid in this case) is missing.

There is a similar bug report - #1935552: PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column - so maybe this report could be considered a duplicate, but it's easier to reproduce this bug ...

Steps to reproduce:
1) Install the view_unpublished module or any other module that implements node grants. (I'm using a totally stripped down version of view_unpublished, which I have called entityreference_test. I'll be happy to attach it.)
2) Create two content types A and B, and add an entityreference field on content type A.
3) Configure the entityreference widget to have B as "Target bundles" and to sort on "A field attached to this entity".
4) Go to example.org/?q=node/add/a (as a user without "bypass node access" permission) and by greeted with the following error:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_data_field_ipaddress0.nid' in 'where clause': SELECT DISTINCT field_data_field_ipaddress0.entity_type AS entity_type, field_data_field_ipaddress0.entity_id AS entity_id, field_data_field_ipaddress0.revision_id AS revision_id, field_data_field_ipaddress0.bundle AS bundle
FROM 
{field_data_field_ipaddress} field_data_field_ipaddress0
WHERE  (field_data_field_ipaddress0.deleted = :db_condition_placeholder_0) AND (field_data_field_ipaddress0.entity_type = :db_condition_placeholder_1) AND (field_data_field_ipaddress0.bundle IN  (:db_condition_placeholder_2)) AND(( ( EXISTS  (SELECT na.nid AS nid
FROM 
{node_access} na
WHERE (( (na.gid = :db_condition_placeholder_3) AND (na.realm = :db_condition_placeholder_4) )OR( (na.gid = :db_condition_placeholder_5) AND (na.realm = :db_condition_placeholder_6) ))AND (na.grant_view >= :db_condition_placeholder_7) AND (field_data_field_ipaddress0.entity_id = na.nid) )) AND (field_data_field_ipaddress0.entity_type = :db_condition_placeholder_8) )OR (field_data_field_ipaddress0.entity_type <> :db_condition_placeholder_9) )AND ( EXISTS  (SELECT na.nid AS nid
FROM 
{node_access} na
WHERE (( (na.gid = :db_condition_placeholder_10) AND (na.realm = :db_condition_placeholder_11) )OR( (na.gid = :db_condition_placeholder_12) AND (na.realm = :db_condition_placeholder_13) ))AND (na.grant_view >= :db_condition_placeholder_14) AND (field_data_field_ipaddress0.nid = na.nid) )) 
ORDER BY field_data_field_ipaddress0.field_ipaddress_start ASC; Array
(
    [:db_condition_placeholder_0] => 0
    [:db_condition_placeholder_1] => node
    [:db_condition_placeholder_2] => article
    [:db_condition_placeholder_3] => 0
    [:db_condition_placeholder_4] => all
    [:db_condition_placeholder_5] => 1
    [:db_condition_placeholder_6] => view_unpublished_content
    [:db_condition_placeholder_7] => 1
    [:db_condition_placeholder_8] => node
    [:db_condition_placeholder_9] => node
    [:db_condition_placeholder_10] => 0
    [:db_condition_placeholder_11] => all
    [:db_condition_placeholder_12] => 1
    [:db_condition_placeholder_13] => view_unpublished_content
    [:db_condition_placeholder_14] => 1
)
 in field_sql_storage_field_storage_query() (line 585 of ../modules/field/modules/field_sql_storage/field_sql_storage.module).

Notice how we have on correct comparison "field_data_field_ipaddress0.entity_id = na.nid" and on wrong "field_data_field_ipaddress0.nid = na.nid".

5) If you change to sort on "A property of the base table of the entity" the problem is gone since the base table no longer isn't the field table.

PS! Could this be a core bug in the node_access function:

$nids = db_or()->condition('nid', $node->nid);

Comments

hansfn’s picture

Issue summary: View changes

Fixed two typos in the issue summary.

hansfn’s picture

Issue summary: View changes

Another typo.

hansfn’s picture

Issue summary: View changes

Yet another typo.

hansfn’s picture

Issue summary: View changes

Fixed SQL dump.

JonMcL’s picture

I can confirm that this is happening and related to the views_unpublished module. Disabling views_unpublished allows entity_reference to work correctly. Can also confirm that the problem is with sort by "A field attached to this entity" and there is no problem with sort by "A property of the base table of the entity".

Cadencia’s picture

I've just created another issue that (on reflection) appears to be linked to this - or, at least, the symptoms of my issue are identical to these, and I suspect the pre-requisite causes of my issue are all present here. Namely, opation by a non-administrator, sorting entity reference by field attached to entity, and some form of node-access in play.

So - to cross reference: link to #2245827

kliban’s picture

I do not have the views unpublished module installed, and I still get the error. As far as I understand, it is a slight incompatibility between access control and Entity Reference module.

I commented on Cadencia comment with whatever analysis of the code I could make, if this be of any help. #2245827.

Still, the only solution I think of is the third one given by dhansen on this other issue #1973952:

  • Drop the sort on the field. This skips the issue entirely, but, you know, no sort.
  • Change the sort to a property of the base table of the entity. Created date is handy for instance.
  • If you absolutely must have the sort based on a field of the referenced entity, you can change the Mode from Simple to Views. This of course requires that you have Views installed and have created an entity reference view. I have no tested this solution myself, but I am reasonably certain it should work as it operates outside of Entity Reference.
igorski’s picture

I can confirm that using a view is a working.
But still, it’s a only a workaround and this bug should be fixed.

Anybody’s picture

Priority: Normal » Major

I can confirm this HUGE HUGE bug still exists. It breaks functionality and it can be easily reproduced by sorting on a integer field "weight" for example...

Is there a maintainer who may have a look what's wrong? This is really essential and breaking things.

BTW I'm NOT using views_unpublished module!

Zekvyrin’s picture

This is a 99.9% a duplicate as mentioned in the description.

Happened to me as well and also "solved" it by removal of ordering, but that isn't exactly "solving it".
Hope it will be resolved somehow..

For me the strange thing is that it looks for the 'nid' field although that fields now have 'entity_id' ...

DigitalFrontiersMedia’s picture

Status: Active » Closed (duplicate)

I think this is a duplicate of #1920998: Unknown Column Error for All Users Except User 1 When Sort Applied To Simple Entity Selection Mode. Someone submitted a patch there to try.