I've been trying to get a comment entity reference field set up in a node, but every time I try the following things happen:
1. I go to my content type's 'Manage fields' area.
2. I create a new entity reference field.
3. On the next screen, I select Comments for the target type and then select the comment bundles below. I leave the Mode as Simple.
4. When I click Save field settings, I get 2 conflicting messages. The first says the fields has been updated (highlighted in green). The second message is a PDOException:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'comment.node_type' in 'where clause': SELECT comment.cid AS entity_id, :entity_type AS entity_type, NULL AS revision_id FROM {comment} comment INNER JOIN {node} n ON n.nid = comment.nid WHERE (comment.node_type IN (:db_condition_placeholder_0)) ; Array ( [:db_condition_placeholder_0] => comment_node_article [:entity_type] => comment ) in EntityFieldQuery->execute() (line 1145 of /var/www/d7/includes/entity.inc).

Based on this error, I'm not sure if the problem lies with Entity Reference or Core. I've tried this on a fresh install, with only Entity Reference and its dependencies enabled with the same results. Any ideas? Thanks.

Comments

sprocketman’s picture

Status: Active » Closed (cannot reproduce)

I've tried this again with another fresh install of D7 and now I'm having no problems. So, closing this issue.

sprocketman’s picture

Status: Closed (cannot reproduce) » Needs review
StatusFileSize
new931 bytes

Ugh...sorry to reopen, but I was mistaken in my previous comment. The bug still remains. Basically, the module is trying to override a faulty query generated by core (I believe) in EntityReference_SelectionHandler_Generic.class.php related to the fact that the comment module sets the entity key "bundle" to "node_type" in comment_entity_info(). When entityreference tries to build a list of entity bundles for the comment type, the initial query attempts to look for a field (node_type) in the comment table that doesn't exist. In your alteration, you were looking for $condition['field'] = 'node_type' (line 453), but instead it should look for $condition['field'] = 'comment.node_type'. A patch is included here.

Status: Needs review » Needs work

The last submitted patch, entityreference-queryalter-1892742-2.patch, failed testing.

sprocketman’s picture

Version: 7.x-1.0 » 7.x-1.x-dev
Status: Needs work » Needs review
StatusFileSize
new931 bytes

Sorry, I think I needed to change the version to the -dev.

Status: Needs review » Needs work

The last submitted patch, entityreference-queryalter-1892742-3.patch, failed testing.

sprocketman’s picture

StatusFileSize
new939 bytes

Whoops, think I need to remove --no-prefix from my git diff statement. Sorry, a little new to this.

sprocketman’s picture

Status: Needs work » Needs review
StatusFileSize
new939 bytes
czigor’s picture

Status: Needs review » Reviewed & tested by the community

Works great for me, thanks!

willowdigit’s picture

Issue summary: View changes

Works for me as well, thank you.

Whys has this not been committed? Is it because it is a fix for a faulty query in core as suggested by @sprocketman#2?

franz’s picture

This looks good, fixed error. I think it should get committed.

pol’s picture

Same problem here when trying to run: drush ter-rebuild from Taxonomy Entity Index.

The patch doesn't seems to fix the problem either.

[lux@s-root-01 entityreference]$ drush tei-rebuild
WD php: PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'comment.node_type' in 'where clause': SELECT COUNT(*) AS expression                                     [error]
FROM
(SELECT 1 AS expression
FROM
{comment} comment
WHERE  (comment.cid > :db_condition_placeholder_0) AND (comment.node_type IN  (:db_condition_placeholder_1, :db_condition_placeholder_2, :db_condition_placeholder_3,
:db_condition_placeholder_4)) ) subquery; Array
(
    [:db_condition_placeholder_0] => 0
    [:db_condition_placeholder_1] => comment_node_page
    [:db_condition_placeholder_2] => comment_node_event
    [:db_condition_placeholder_3] => comment_node_forum
    [:db_condition_placeholder_4] => comment_node_article
)
 in EntityFieldQuery->execute() (line 1154 of /opt/drupcore/includes/entity.inc).
PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'comment.node_type' in 'where clause': SELECT COUNT(*) AS expression
FROM
(SELECT 1 AS expression
FROM
{comment} comment
WHERE  (comment.cid > :db_condition_placeholder_0) AND (comment.node_type IN  (:db_condition_placeholder_1, :db_condition_placeholder_2, :db_condition_placeholder_3, :db_condition_placeholder_4)) ) subquery; Array
(
    [:db_condition_placeholder_0] => 0
    [:db_condition_placeholder_1] => comment_node_page
    [:db_condition_placeholder_2] => comment_node_event
    [:db_condition_placeholder_3] => comment_node_forum
    [:db_condition_placeholder_4] => comment_node_article
)
 in EntityFieldQuery->execute() (line 1154 of /opt/drupcore/includes/entity.inc).
Drush command terminated abnormally due to an unrecoverable error.                                                                                                                    [error]
[lux@s-root-01 entityreference]$
jami’s picture

The patch in #7 fixed the error. Thanks!

MorinLuc0’s picture

Status: Reviewed & tested by the community » Needs work

Tested on the 7.x-1.x-dev version with drupal core 7.50 and error still exist but can't be validated till test are provided.

tky’s picture

I got a similar PDOException error too.

But the message did not appear when I add a comment reference field as sprocketman did in the first place. The error message shows every time in the log while I get into a node create form via entityreference prepopulate link. What I got in front end is always an "Site encountered an Unexpected Error" message.

If the target entity is replaced from Comment to Content, the reference filed works fine and gets no any error message.

My Drupal is 7.53, has entityreference 7.x-1.x-dev (2017-Apr-13) and entityreference_prepopulate 7.x-1.7 installed

mennovdheuvel’s picture

Getting this same error now:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'comment.node_type' in 'where clause': SELECT comment.cid AS entity_id, :entity_type AS entity_type, NULL AS revision_id FROM {comment} comment INNER JOIN {node} n ON n.nid = comment.nid WHERE (comment.cid IN (:db_condition_placeholder_0)) AND (comment.node_type IN (:db_condition_placeholder_1, :db_condition_placeholder_2)) AND (n.status = :db_condition_placeholder_3) ORDER BY comment.subject ASC; Array ( [:db_condition_placeholder_0] => 2700294 [:db_condition_placeholder_1] => comment_node_forum_topic [:db_condition_placeholder_2] => comment_node_poll [:db_condition_placeholder_3] => 1 [:entity_type] => comment ) in EntityReference_SelectionHandler_Generic->validateReferencableEntities() (line 198  /docroot/sites/all/modules/contrib/entityreference/plugins/selection/EntityReference_SelectionHandler_Generic.class.php).

This happens after trying to reopen comments on a node after it had been closed before, and another node holds a reference to a comment on the node. Core 7.59 and entityreference 7.x-1.5.

Is there any easy workaround?

mennovdheuvel’s picture

Right. I got around this error using the following patch for EntityReference_SelectionHandler_Generic.class.php:

Line 465 was:

if ($key !== '#conjunction' && is_string($condition['field']) && $condition['field'] === 'node_type') {

and I changed this to:

if ($key !== '#conjunction' && is_string($condition['field']) && (($condition['field'] === 'node_type') || ($condition['field'] === 'comment.node_type'))) {

I'm not sure if the field ever is just "node_type" without the table name and, if so, where the variations come from. That's for someone else to figure out.

tky’s picture

#16 work for me.

Thanks a lot.

delacosta456’s picture

hi #7 worked for me too

Thanks

mimalef70’s picture

Hi, Whys has this not been committed ?

Thanks

delacosta456’s picture

hi @mimalef70

May be because patch has not gotten enough test by community.