Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
When saving an entity containing a image or file subfield inside a multifield, I get the following PDOException:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column fid; cannot be null: INSERT INTO {file_usage} (fid, module, type, id, count) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4); Array(
[:db_insert_placeholder_0] =>
[:db_insert_placeholder_1] => file
[:db_insert_placeholder_2] => multifield
[:db_insert_placeholder_3] => 122
[:db_insert_placeholder_4] => 1
) in file_usage_add() (line 697 of /var/www/develop/dist/webroot/includes/file.inc).
When file_field_insert()
is called for the image/file subfield, $items
contains one iten with a NULL
fid
. This NULL-fid item is added by multifield_item_serialize()
called at the end of multifield_field_presave()
.
Comments
Comment #1
pbuyle CreditAttribution: pbuyle commentedI tried forcing the call to
multifield_field_presave()
inmultifield_field_insert()
. While it does callfile_field_presave()
with a NULL-fid item (which is removed), this does not solve the issue. When_multifield_field_attach_insert()
is called for the image field, the NULL-fid item is still present.Comment #2
pbuyle CreditAttribution: pbuyle commentedThe NULL-fid item is added in
multifield_item_serialize()
called at the end ofmultifield_field_presave()
.Comment #3
pbuyle CreditAttribution: pbuyle commentedThe bug was introduced in commit 0a4d448b6bd615bc80bc8d148d67ab2ff82956de (when using a checkout at commit 64c468327c65b6038d073a1997a1fafbd1328114, the issue disappear).
Comment #4
Dave ReidIf we wrapped an else condition around
$item[$subfield_name . '_' . $column] = &$item[$subfield_name][LANGUAGE_NONE][0][$column];
would that help?Comment #5
pbuyle CreditAttribution: pbuyle commentedThe issue in
multifield_item_serialize()
is with the following codeFor a file subfield, this create a value like
array('fid' => NULL, ...)
at$item[$subfield_name][LANGUAGE_NONE][0]
. Whenfile_field_insert()
is called, it invokesfile_usage_add()
with that value, which in turn try to insert a{file_usage}
row with a NULLfid
.Normally, for a file field,
file_field_presave()
will remove an item with a NULLfid
. It actually does it for a file subfield, butmultifield_item_serialize()
add it back when called at the end ofmultifield_field_presave()
.Comment #6
sistro CreditAttribution: sistro commentedI've the same issue... any solution?!
Comment #7
sistro CreditAttribution: sistro commentedHere I'am... now I can describe my issue better.
I've got the error message (and the content not saved) when in the multifield I've a image field.
I've:
- Title
- Description
- Image
- Tablefield
If i leave blank all the field no problems.
If I have another field without image I've the error!
Also I've an issue with tablefield, when I try to rebuild tables but I've discovered that it is a tablefield module you can find the patch here: https://www.drupal.org/node/1288510#comment-9183039
This is a great module but I can't use it with this two errors!
Comment #8
rgrms CreditAttribution: rgrms commentedIs there any progress on solving this issue?
Comment #9
jerodfritz CreditAttribution: jerodfritz commentedI have this same problem
Comment #10
rgrms CreditAttribution: rgrms commentedGuys, I know that normally I have to be shot in head for such solution, but I faced a situation when this problem had to be fixed on working website within 5 minutes.
SO, below is extremely dirty workaround.
1) Since Multifield has no validation on empty image subfields, I manually created an image at public directory and added a managed_file entry into database with some fid to give it something than NULL for fid.
2) I replaced:
to this:
Comment #11
mix359 CreditAttribution: mix359 commentedMaybe is not the right solution, but I've changed the multifield_item_serialize() code, adding this
before this
The idea is to check if the field doesn't have data ([0] doesn't exist on a field if it doesn't have data) and in that case return NULL...
don't know if there's any problem with the fact that it isn't a reference to a variable...
Hope can help :)
Comment #12
MixologicNice find @Mongolito404, Dave's Comment in #4 is exactly where the issue is.
The reference assignment alters the existing data such that if it doesnt already have a value, one gets added to it:
so:
$item[$subfield_name . '_' . $column] = &$item[$subfield_name][LANGUAGE_NONE][0][$column];
Not only assigns a value to
$item[$subfield_name . '_' . $column]
, but it *also* adds array elements to$item[$subfield_name][LANGUAGE_NONE][0]
if they didnt exist before.Attached is a patch that wraps the assignment in an if, as well only assigns the defaults to the subfields if it has a default set. (might break other things.. havent fully tested)
Comment #13
ysmith CreditAttribution: ysmith commentedHi Mixologic:
I use the deploy module to pass the content from one site to another. Before this patch, using multifield (one textbox field and one image field), the content in dev site deploy normal without errors, but the content not arrive to production page. Now with your patch, the content arrive to production page, text fields are filled, but the field image are empty. In error log not show nothing.
Help!
Comment #14
Alienpruts CreditAttribution: Alienpruts commentedHi Mixologic,
I had the same issue as above, multifield with two file subfields (one image and one file).
After applying your patch : no luck, allthough the error message now is accompagnied by a notice :
I'm guessing this is specific to my use case and I'll keep looking for an answer, but I thought you should at least know :)
Tnx for your work.
makkelijkegroenten
EDIT : i'm goofin' : there is only one file field, no image field. Sorry for the confusion.
EDIT2 : strange, all seemed to be working, until i started with Entity Translation of these fields. The error returned there.
Comment #15
alisaeedi CreditAttribution: alisaeedi commented#12 worked for me, Thanks Mixologic
Comment #16
JamesAn CreditAttribution: JamesAn commentedI'm confirming the exact error outcome in #2327317-14: Integrity constraint violation when saving multifield with a file subfield (i.e. same PHP errors occuring on the same specific PHP files and lines)
, but I'm having trouble isolating the source of the bug -- even to just the culprit field or field type, except that I know either or both of image field and file field are involved, as well as multifield. The client site has multilingual requirements which will be implemented using Entity Translations, but that hasn't even been turned on yet.The entity architecture affected by this is a bit complicated in that the entity type has a polymorphic multifield bundle that has a list_text subfield that controls the set of fields that can be both editted by content creator roles and viewed at large by everyone through conditional_fields. I've confirmed that the bug reliably occurs even when field dependencies, but the error seems to reliably occur and also not occurring when the same subfield instances are in place, depending on the ordered steps I've taken to reinstantiate them in the multifield bundle...In isolated debugging tests, the error doesn't seem to occur when an entity type only a multifield, which is made up of none of, either, or both of: an image or file field (i.e. respectively, a reference to the image bundle of file entities and a reference to any bundle of file entities) -- and, of course, a title or title_field via the Title module). The bug also doesn't trigger when the entity type itself has either or both an image and file field which is the same reference fields instantiated in the multifield or instances of distinct fields entirely.Will try some other ideas to trace the source of what's going on here.I managed to rewrite the logic in multifield_item_serialize() to correctly serialise the respective columns of the subfields while taking into account default values when the user submits no value for the subfield and the fact that this function is executed multiple times (and the serialisation should only occur the first function call for each subfield.
Will wrap this into a patch shortly. TBC...
Comment #17
JamesAn CreditAttribution: JamesAn commentedVoici. Le patch.
I've modified the logic itself in how the
multifield_item_serialize()
function determines what value to assign to the multiple$item[$subfield_name . '_' . $column]
that correspond to the subfield's actual database table columns with the following logic flow:$item[$subfield_name . '_' . $column]
is already set (i.e. been assigned a value in an earlier invocation ofmultifield_item_serialize()
.$column
as derived from the field's definition constructed byfield_info_field()
also exists in the $item array that contain the actual values that make up the subfield, copy the matching value to$item[$subfield_name . '_' . $column]
to be saved in the database.$item[$subfield_name . '_' . $column]
.$item[$subfield_name . '_' . $column]
.There is no catch-all else statement as any case that falls through these control statements is invalid/inconsistent (or I'm misunderstanding this data model). The last elseif statement shouldn't be replaced with a general else statement as it acts as assertion as that last elseif's condition should always trigger if the specific subfield column hasn't satisfied any earlier conditions.
Also, unset
$item[$subfield_name]
as it's now broken up and entirely represented by the various$item[$subfield_name . '_' . $column]
, and is therefore redundant/no longer of use.Preliminary testing (not exhaustive at all) shows this patch resolving this issue insofaras the PDOException is no longer triggered, the inputted, default, or empty values are saved in that order of precedence as expected. This doesn't appear to cause any regressions with respect to non-image/-file fields either. I also haven't tested this in a multilingual environment, but that'll happen in the coming days at the latest.
Comment #18
LonitaD CreditAttribution: LonitaD commentedThe patch in #17 fixed it for me.
Comment #19
acolden CreditAttribution: acolden commentedThe patch in #17 fixed it for me too.
Comment #20
JamesAn CreditAttribution: JamesAn commentedWe've been depending on this patch for over two week now without issue. With these two confirmations, can we RBTC this?
Comment #21
Dave ReidThis needs some tests to prove the bug and fix is good. I know the 7.x-1.x branch tests are currently failing, but we still need tests for this.
Also the code style needs to be fixed to use the Drupal coding standards, especially around the if statements.
Comment #22
Dave ReidComment #24
rituraj.gupta CreditAttribution: rituraj.gupta commented#17 seems to be working for me too!
Comment #28
doakym CreditAttribution: doakym commentedThe patch in #17 fixed it for me.
Comment #29
jenlamptonRerolled the patch from #17 with correct coding standards. Still needs tests, but I'm happy to have the bug fixed - thanks for the hard work all! :)
Comment #30
Lepakshi Reddy Gollapalli CreditAttribution: Lepakshi Reddy Gollapalli commentedStill shows "The file used in the {file field name} field may not be referenced." while updating the content. While insert the content its success, but fails while update content.
Comment #31
Lepakshi Reddy Gollapalli CreditAttribution: Lepakshi Reddy Gollapalli commentedif comment the line unset($item[$subfield_name]); it works for me.
//unset($item[$subfield_name]);
Comment #32
justafishThe patch in #29 works well for me, thanks!
Comment #33
justafishSpoke too soon... resaving the node gives the error
The file used in the Image field may not be referenced.
Comment #34
juampynr CreditAttribution: juampynr at Lullabot commented@justafish, that error is related with #2054243: Ensure file usage is tracked appropriately for image and file fields.
Comment #35
irowboat CreditAttribution: irowboat commentedTemporarily solved this by commenting this line out in core (NOT RECOMMENDED but works as a very quick temp fix):
form_error($element, t('The file used in the !name field may not be referenced.', array('!name' => $element['#title'])));
Another thing, patch #29 has these lines:
--- a/multifield/multifield.module
+++ b/multifield/multifield.module
Patch won't apply in multifield root with this, you need to remove the dir name.
Comment #36
Lepakshi Reddy Gollapalli CreditAttribution: Lepakshi Reddy Gollapalli commentedThis patch created on top of #29 patch, It resolves the "The file used in the {file field name} field may not be referenced." issue.
Comment #38
noman_297 CreditAttribution: noman_297 commented@jenlampton patch #29 worked form me.Thanks
Comment #39
lachris CreditAttribution: lachris commentedThe "The file used in the {file field name} field may not be referenced." error is because for each file uploaded, a record needs to be added to the "file_usage" table. I created a patch that runs file_usage_add before each field is saved. There might be a more appropriate place to put it. But this works, and I need to continue working on my project. Anyone willing to take over and improve, be my guest :)
@jenlampton, I included your #29 patch in mine as well if you don't mind.
Comment #40
lachris CreditAttribution: lachris commentedMy patch #39 gets the "Integrity contraint violation" errors when creating a new node, because it couldn't find the node id, which hasn't been generated at that point. A workaround is to call file_usage_add() every time we open the node/%/edit page. Now it works both when creating a node and updating a node.
Comment #41
loparr CreditAttribution: loparr commented#17 works for me. Thank you
Comment #42
detroz CreditAttribution: detroz commented.
Comment #43
badrange CreditAttribution: badrange at Wunder commentedAttaching a version of the patch from #29 which should work also from drush make files. The patch in 29 would be needed to be applied from the modules directory, and not the multifield module's own directory.
Quick test with patch from #29 and "#2054243: Ensure file usage is tracked appropriately for image and file fields." together seem very promising.
Comment #44
anjjriit CreditAttribution: anjjriit commentedPatch #40 work for me too
Comment #45
detroz CreditAttribution: detroz commentedI apply the solution proposed by badrange (see his comment : https://www.drupal.org/node/2327317#comment-10445483).
But I have a little bug with empty array.
I have a content with a "link" field.
No link attribute was attributed. So this array is empty.
In the request, the empty array was remplaced by nothing.
Ans MySQL is not agreed to have no value for "field_multifield_field_link_attributes" column...
[Update 19/01] After tests, every "attributes" array from "link" field provokes a SQL exception.
Because the array is not serialized...
To correct that, I alter the function multifield_item_serialize like that:
Comment #46
takirami CreditAttribution: takirami commentedPatch #43 worked for me.
However I do get a "The file used in the Picture field may not be referenced." error when editing nodes with images in multifield instances.
Comment #47
takirami CreditAttribution: takirami commentedNevermind, patch #40 works for me.
Comment #48
Bohus UlrychHi all,
thank you for this great module which is the only one of such kind which works with entity translation (at least the way how I need).
But I have also issue to attach image. I tried patches #40 and #43 but they didn't worked.
I'm currently using solution #11 by mix359 which seems to be working well.
Comment #49
jenlamptonPatch in #40 is working for me as well. Thanks for the updates, everyone!
edit: Oh, just read the last comment. Not sure if this is RTBC yet, @Bohus Ulrych can you describe the problems you are having after using the patch in #40?
Comment #54
jayturn CreditAttribution: jayturn commentedThe patches in #40 and #43 didn't work for me. I've added a patch that removes the file fields from the $pseudo_entity but keeps it in the original $item array. That way the field entity isn't affected but the multifield table is updated accordingly.
Comment #55
glynster CreditAttribution: glynster commentedPatch 40 worked worked for us @jayturn, as your patch was not successful :(
Comment #56
badrange CreditAttribution: badrange at Wunder commentedA lot of water has flowed under the bridge since I last touched this issue, and I realised to my own surprise that the project in question had this issue - and not my own patch. I have a feeling that it never left the testing stage of the project and thus did not get put into production.
Just now, I upgraded the multifield module to the latest git revision and added jayturn's patch from #54, and I am able to save those nodes that caused the PDOException upon saving, so crossing my fingers that our solution is solved with that one.
glynster: In what way doesn't that patch work for you?
and jayturn: in what way didn't 40 or 43 work for you?
Would be great to move this issue forward and get a proper fix committed at some point.
Comment #57
glynster CreditAttribution: glynster commented@badrange I received the same initial error with the path from #54. So patched or not the same error. Once patch #40 was installed all worked as expected.
Comment #58
ArnaudDabouis CreditAttribution: ArnaudDabouis commented#40 works for me too, #54 leaves the same error.
Comment #59
Dave ReidI think I've got tests and a fix that might work. This will need real-world testing.
Comment #60
Dave ReidRemoving the debug() calls.
Comment #61
Dave ReidPatch showing the test only which should fail with the well-known PDO Exception errors.
Comment #62
Dave ReidForgot the .info file change for the test-only patch.
Comment #64
Dave ReidConfirmed test failure, setting back to needs review for #60. Please test it everyone if you can.
Comment #66
Dave ReidComment #68
Dave ReidOk, I went ahead and committed #60 to 7.x-1.x. I'm confident it fixes the issue.
Comment #69
jenlamptonThanks @Dave Reid!!! :)
Comment #71
darol100 CreditAttribution: darol100 as a volunteer and commentedThank you so much. I just run into this issue right now. And it seem to be working if we are using the 7.x-2.x-dev version.