Column not found (nid) in on clause, using i18n, entityreference, field_sql_storage-Collection of common programming errors

I’m using Drupal 7.14 and Entity API and Reference 1.0-rc3. 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