As per https://drupal.org/node/1569144, the target_id column is now NOT NULL, but this causes a PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 problem if a node is saved without setting a value in the field.

Any recommendations on how to deal with this?

Comments

bsuttis’s picture

Got around it using hook_node_presave()

/**
 * Implements hook_node_presave().
 */
function mymodule_node_presave($node) {
  if(empty($node->field_fieldname['und'][0]['target_id'])) {
    $node->field_fieldname['und'][0]['target_id'] = 0;
}

Not the best but it's working.

yan’s picture

Issue summary: View changes

Same problem here. This happens in the middle of an upgrade process so I'm not really sure where it comes from. But it seems to be this issue. When I save a node with entity reference fields that have no value, I get:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'field_NAME_target_id' cannot be null: INSERT INTO {field_data_field_NAME} (entity_type, entity_id, revision_id, bundle, delta, language, field_NAME_target_id) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6); Array ( [:db_insert_placeholder_0] => node [:db_insert_placeholder_1] => 126236 [:db_insert_placeholder_2] => 155050 [:db_insert_placeholder_3] => audio [:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => und [:db_insert_placeholder_6] => ) in field_sql_storage_field_storage_write() (line 514 of /modules/field/modules/field_sql_storage/field_sql_storage.module).

indigoxela’s picture

Just saw the same issue with PDOExceptions on an upgraded Drupal (6 to 7).
Manually saving all entityreference fields (field settings, not nodes!) just once fixed the problem.

So this might be just another issue with upgrades, not specific to entityreference.

torotil’s picture

Category: Support request » Bug report

I just did some extensive debugging on this. Here is what I found:

If you save an entity without any field items, then field_default_insert() adds the default items from the field instance config. It is called in field_attach_insert() and thus runs after the whole field widget processing (ie. when all empty values are already removed). However when editing field instance the default value is stored to the instance settings without being filtered by _field_filter_items(). So if you don’t select a default value in a single item instance then [0 => [target_id => NULL]] is inserted by field_default_insert(). This leads to the error.

So steps to reproduce are:

1. Have a bundle with a single value entityreference field with “None” as default value.
2. Create a new entity of that bundle without any value for the entityreference field.
3. Save the entity. → Error.

This is a Drupal core bug as it concerns all fields where the form API output of the field widget is not directly a valid field item.

torotil’s picture

torotil’s picture

torotil’s picture

Ok it seems for me this was only triggered by the patch in #2852019: D7 Default text formats are not saved properly without accompanying values.

I have updated the patch there to resolve this issue.