When you create a new entity reference field in the UI, field_create_field() runs on field_ui_field_overview_form_submit() which runs before you can edit the settings for that field. This means that for every entity reference field that is created, everything in entityreference_field_schema() is hard-coded to the defaults provided in hook_field_info(). This means that the foreign key information is completely incorrect, which can break modules like http://drupal.org/sandbox/drothstein/1775816 that rely on foreign key information in fields being correct.

I'm not sure this can be fixed from core, but entityreference will need to fix all the current schemas for existing fields in an update hook.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dave Reid’s picture

Set as major as I've got to assume this messes up CTools relationships as well.

Dave Reid’s picture

Dave Reid’s picture

Dave Reid’s picture

mansspams’s picture

Temporary workaround: http://drupal.org/node/1340748#comment-5806046 (resave all entityreference fields after patch (click on edit, then on save without changing stuff)).

This not only messes up CTools relationships, but blocks developers of term related modules to target Entity reference fields cleanly (see #1981262: Entity reference support for Term Merge module).

joachim’s picture

This also causes Features containing entref fields to report as overridden all the time, with this sort of diff:

       'foreign keys' => array(
-        'node' => array(
+        'other_entity' => array(
           'columns' => array(
-            'target_id' => 'nid',
+            'target_id' => 'eid',
           ),
-          'table' => 'node',
+          'table' => 'other_entity',
         ),
Damien Tournoud’s picture

Status: Active » Closed (duplicate)

Core bug, see #4.

Dave Reid’s picture

Status: Closed (duplicate) » Postponed

Entity reference should still have an update hook to fix the field definitions and foreign key definitions once this is fixed in core.

mansspams’s picture

Status: Postponed » Active

So, then active.

Dave Reid’s picture

The following core patch needs to be committed before this issue can be worked on: #1416506: Field schema foreign keys support is broken

jpstrikesback’s picture

Is this a problem in 8.x as well?

Dave Reid’s picture

It has already been fixed in 8.x with #1416506: Field schema foreign keys support is broken

jpstrikesback’s picture

Status: Active » Needs review
FileSize
1.44 KB

OK, so for the update function, something like this?

joachim’s picture

+++ b/entityreference.install
@@ -161,4 +161,39 @@ function entityreference_update_7002() {
+    $sql_field = field_read_field($field['field_name']);

You can say $field_name here, which is easier to read.

There's also a few tweak needed with comments - a full stop missing & a line that needs wrapping.

Other than that, looks fine.

I tried the patch on a site where Features kept reporting my entityreference field as overridden. The fields were correctly fixed in the DB, and the feature then required an update.

jpstrikesback’s picture

Thanks for the review Joachim, here are those changes.

joachim’s picture

Status: Needs review » Reviewed & tested by the community

Cool!

Sweetchuck’s picture

Status: Reviewed & tested by the community » Needs review

The latest patch is works well for an already running sites, but if you have a brand new site and you apply the patch before you enable the entityreference module then the problem with the 'foreign key' is still exists.

bradjones1’s picture

Status: Needs review » Reviewed & tested by the community

With respect to the last comment in #17 - see #8. The genesis of this problem was fixed in core, but if you have fields that were created before that was incorporated into your install, the update hook needs to run on them. So I think this is appropriately RTBC, if that was your only concern.

Leeteq’s picture

Can this at least be committed to -dev now?

anawillem’s picture

I also wish this would get placed in the codebase. We are using:
Features 7.x-2.2
EntityReference7.x-1.1

and I am noticing this patch is not in the codebase...

I tried applying the patch, and I get index errors:

rodrigoaguilera’s picture

Status: Reviewed & tested by the community » Needs work

The patch applies cleanly to 7.x-1.x

I experience this on sites installed from scratch that use features. Since this is just an update function it doesn't fix the problem and I have to build my features twice so I don't have them overridden.

Dave Reid’s picture

Status: Needs work » Reviewed & tested by the community

Yes, you would have to update all of your Feature exports after the update hook runs. There's no way for the update hook to do that for you.

rodrigoaguilera’s picture

I develop sites installing the drupal profile every time so there's never update_hook involved.

I export the entity reference field with features and when I install the site again the feature is overriden. Once I re-export it is fine again.

Should I open a different issue for this?
What I get with:
drush features-diff myfeature

Component type: field_base
      'cardinality' => 1,
      'field_name' => 'field_course_category',
<     'foreign keys' => array(
<       'taxonomy_term_data' => array(
<         'columns' => array(
<           'target_id' => 'tid',
<         ),
<         'table' => 'taxonomy_term_data',
<       ),
<     ),
      'indexes' => array(
        'target_id' => array(
andriuzss’s picture

@rodrigoaguilera, I think temporary solution in this case is to update all entity reference fields with new foreign keys, recreated feature with those changes.
And for new fields use hook_field_update_field, so set proper foreign key.

/**
 * Implements hook_field_update_field().
 */
function hook_field_update_field($field) {
  // Load the base table configuration from the cache.
  $base_tables = variable_get('entityreference:base-tables', array());

  if ($field['type'] != 'entityreference') {
    // Not an entity reference field.
    return;
  }

  // Read in the field config
  $sql_field = field_read_field($field['field_name']);

  // Create a foreign key to the target entity type base type, if available.
  $entity_type = $sql_field['settings']['target_type'];
  if (isset($base_tables[$entity_type])) {
    list($base_table, $id_column) = $base_tables[$entity_type];
    $schema['foreign keys'][$base_table] = array(
      'table' => $base_table,
      'columns' => array('target_id' => $id_column),
    );
  }

  // Update foreign keys to actually contain the proper entity foreign keys if they differ.
  if ($sql_field['foreign keys'] != $schema['foreign keys']) {
    $sql_field['foreign keys'] = $schema['foreign keys'];
    field_update_field($sql_field);
  }
}
Leeteq’s picture

(Crossreferencing so this issue is visible on the fixed core issue as well.)

alex.skrypnyk’s picture

Cross-referencing with an issue in Features queue: https://www.drupal.org/node/2419479

Basically, starting Features 2.4 'foreign keys' are no longer exported as a part of field base.

DamienMcKenna’s picture

Sam152’s picture

I think this is still a problem when using features. Since foreign keys are removed from the field config, I believe they are empty when imported. I'm also using an install profile workflow and this patch wouldn't impact the site at all on a fresh installation.

delacosta456’s picture

hi
should apply this patch to entityreference module?

japerry’s picture

Rerolled for the most recent version of entity reference.

spotzero’s picture

So the patch added a hook_update that fixes a problem that doesn't occur any more.

Given the age of this issue, does anyone still want this committed? If so, I'll test and commit it.

joelpittet’s picture

Status: Reviewed & tested by the community » Needs work

This probably needs to be entityreference_update_7101 now?

gaurav.kapoor’s picture

Status: Needs work » Needs review
FileSize
1.71 KB

Will be useful and yes entityreference_update_7101().

Status: Needs review » Needs work

The last submitted patch, 33: 1969018-update-field-config-foreign-keys-33.patch, failed testing.

gaurav.kapoor’s picture

Status: Needs work » Needs review
FileSize
1.45 KB
quotesBro’s picture

quotesBro’s picture