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.
Comment | File | Size | Author |
---|---|---|---|
#37 | 1969018-37-interdiff.txt | 361 bytes | quotesBro |
#37 | 1969018-update-field-config-foreign-keys-37.patch | 1.47 KB | quotesBro |
| |||
#35 | 1969018-35.patch | 1.45 KB | gaurav.kapoor |
| |||
#33 | 1969018-update-field-config-foreign-keys-33.patch | 1.71 KB | gaurav.kapoor |
#30 | 1969018-update-field-config-foreign-keys-30.patch | 1.71 KB | japerry |
|
Comments
Comment #1
Dave ReidSet as major as I've got to assume this messes up CTools relationships as well.
Comment #2
Dave ReidFiled issue against Field Reference Delete: #1969042: foreign key information on entity reference fields is not reliable
Comment #3
Dave ReidFiled issue against core: #1969048: hook_field_schema() cannot vary based on field settings
Comment #4
Dave ReidActual core bug is #1416506: Field schema foreign keys support is broken
Comment #5
mansspams CreditAttribution: mansspams commentedTemporary 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).
Comment #6
joachim CreditAttribution: joachim commentedThis also causes Features containing entref fields to report as overridden all the time, with this sort of diff:
Comment #7
Damien Tournoud CreditAttribution: Damien Tournoud commentedCore bug, see #4.
Comment #8
Dave ReidEntity reference should still have an update hook to fix the field definitions and foreign key definitions once this is fixed in core.
Comment #9
mansspams CreditAttribution: mansspams commentedSo, then active.
Comment #10
Dave ReidThe following core patch needs to be committed before this issue can be worked on: #1416506: Field schema foreign keys support is broken
Comment #11
jpstrikesback CreditAttribution: jpstrikesback commentedIs this a problem in 8.x as well?
Comment #12
Dave ReidIt has already been fixed in 8.x with #1416506: Field schema foreign keys support is broken
Comment #13
jpstrikesback CreditAttribution: jpstrikesback commentedOK, so for the update function, something like this?
Comment #14
joachim CreditAttribution: joachim commentedYou 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.
Comment #15
jpstrikesback CreditAttribution: jpstrikesback commentedThanks for the review Joachim, here are those changes.
Comment #16
joachim CreditAttribution: joachim commentedCool!
Comment #17
SweetchuckThe 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.
Comment #18
bradjones1With 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.
Comment #19
Leeteq CreditAttribution: Leeteq commentedCan this at least be committed to -dev now?
Comment #20
anawillem CreditAttribution: anawillem commentedI 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:
Comment #21
rodrigoaguileraThe 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.
Comment #22
Dave ReidYes, 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.
Comment #23
rodrigoaguileraI 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
Comment #24
andriuzss CreditAttribution: andriuzss commented@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.
Comment #25
Leeteq CreditAttribution: Leeteq commented(Crossreferencing so this issue is visible on the fixed core issue as well.)
Comment #26
alex.skrypnykCross-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.
Comment #27
DamienMcKennaComment #28
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedI 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.
Comment #29
delacosta456 CreditAttribution: delacosta456 commentedhi
should apply this patch to entityreference module?
Comment #30
japerryRerolled for the most recent version of entity reference.
Comment #31
spotzero CreditAttribution: spotzero at Coldfront Labs Inc. commentedSo 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.
Comment #32
joelpittetThis probably needs to be
entityreference_update_7101
now?Comment #33
gaurav.kapoor CreditAttribution: gaurav.kapoor at OpenSense Labs commentedWill be useful and yes entityreference_update_7101().
Comment #35
gaurav.kapoor CreditAttribution: gaurav.kapoor at OpenSense Labs commentedComment #36
quotesBro CreditAttribution: quotesBro commentedComment #37
quotesBro CreditAttribution: quotesBro commented