What may cause Column not found (nid) in on clause? I'm using Drupal 7.14 and Entity API and Reference 1.0-rc3.

And I'm also using i18n, entityreference (not References) and field_sql_storage.

I get this error when saving a node that has some custom fields using references.


  PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column
  'field_data_body0.nid' in 'on clause': SELECT
  field_data_body0.entity_type AS entity_type,
  field_data_body0.entity_id AS entity_id, field_data_body0.revision_id
  AS revision_id, field_data_body0.bundle AS bundle FROM
  {field_data_body} field_data_body0 INNER JOIN {node} node ON
  field_data_body0.nid = node.nid WHERE (field_data_body0.deleted =
  :db_condition_placeholder_0) AND (field_data_body0.entity_type =
  :db_condition_placeholder_1) AND (field_data_body0.bundle IN
  (:db_condition_placeholder_2, :db_condition_placeholder_3)) AND
  (field_data_body0.entity_id IN (:db_condition_placeholder_4)) AND
  (node.language IN (:db_condition_placeholder_5,
  :db_condition_placeholder_6)) ORDER BY field_data_body0.body_value
  ASC; Array ( [:db_condition_placeholder_0] => 0
  [:db_condition_placeholder_1] => node [:db_condition_placeholder_2] =>
  news [:db_condition_placeholder_3] => page
  [:db_condition_placeholder_4] => 25 [:db_condition_placeholder_5] =>
  it [:db_condition_placeholder_6] => und ) in
  field_sql_storage_field_storage_query() (linea 577 di
  /home/morpheu5/web/xdatanet/modules/field/modules/field_sql_storage/field_sql_storage.module).


What bothers me is that, first of all there's a zero appended to the name of the table, and second, if I add that nid column to that table, Drupal is happy and everything seems to work.

I suspect there must have been some update gone horribly wrong, since maintenance is not under my control, and I had to implement some new features working on my local copy which at that time was already completely out of sync with the client's staging and production servers.

I tried a number of patches, and I've gone through the code myself but I'm not this familiar with Drupal.

EDIT

It seems the nid column is getting added at some point in function preExecute in includes/database/select.inc specifically at row 1236

drupal_alter($hooks, $query);


the hooks acting here are


query
query_entity_field_access
query_node_access
query_entityreference


but after a hour long investigation I only came up with a huge headache, and I'm still not sure if I actually found and debugged all of those hooks. As far as I could see, the nid column is not present before the drupal_alter call while it is after, but it's there at the beginning of each hook_query_TAG_alter I was able to find.

I must be missing something.

EDIT 2

The culprit seems to be i18n, specifically i18n_select, more specifically at row 137 of i18n_select.module where it adds the nid column.

EDIT 3

There, I opened a bug: https://drupal.org/node/1679646
The body field should definitively NOT have a nid column in the table. The field_data_body table provided by core will contain an entity_id column which is the reference to the node entity.

2 things I can think of that might be causing this:


You have custom code that's making an erroneous database query in a hook_node_load() function or similar (either directly or using an EntityFieldQuery)
(more likely I think) Core/contributed modules or files have been edited and the changes are only now causing problems.


The best advice I can give is to install the Hacked! module and determine whether or not core/contributed modules have indeed been hacked. If that comes back all clear then you're just looking at an error in some custom code somewhere which will be easy to track down.

If it doesn't come back clear, at least you know what the problem is and you can then decide how best to proceed with getting Drupal back into a proper un-hacked state.

Incidentally, don't worry about the table being referred to as field_data_body0...if you look further down in the query that's just an alias that's being used for field_data_body:

FROM {field_data_body} field_data_body0