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.
During the import of a file of node_export of an existing file, Drupal throws the following error:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'field_resource_file_display' cannot be null: INSERT INTO {field_data_field_resource_file} (entity_type, entity_id, revision_id, bundle, delta, language, field_resource_file_fid, field_resource_file_display, field_resource_file_description) 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, :db_insert_placeholder_7, :db_insert_placeholder_8); Array ( [:db_insert_placeholder_0] => node [:db_insert_placeholder_1] => 2280 [:db_insert_placeholder_2] => 4168 [:db_insert_placeholder_3] => resource [:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => und [:db_insert_placeholder_6] => 1714 [:db_insert_placeholder_7] => [:db_insert_placeholder_8] => ) in field_sql_storage_field_storage_write() (line 448 of /var/www/html/modules/field/modules/field_sql_storage/field_sql_storage.module).
The reason for this error is in node_export.module within the function _node_export_file_field_import_file():
$query = db_select('file_managed', 'f')
->fields('f', array('fid'))
->condition('uri', $file->uri)
->execute()
->fetchCol();
if (!empty($query)) {
watchdog('node_export', 'kept existing managed file at uri "%uri"', array('%uri' => $file->uri), WATCHDOG_NOTICE);
$file = file_load(array_shift($query));
}
$file = file_save($newfile);
file_load() does not load all the necessary fields, and certain fields such as "display" get left out. If these attributes are missing field_sql_storage will throw an error, because the attribute can't be null.
A quick fix to this, is to merge the original file attributes from the import, with the loaded one:
$newfile = (object)array_merge((array)$oldfile,(array)$file);
Patch is attached, please review my fix.
Comment | File | Size | Author |
---|---|---|---|
#5 | node_export-1911638_2.patch | 522 bytes | cinnamon |
#2 | node_export-1911638.patch | 890 bytes | kenorb |
#1 | fid_overwrite_patch.diff | 837 bytes | olklein |
missing_file_attributes_patch.diff | 879 bytes | olklein |
Comments
Comment #1
olklein CreditAttribution: olklein commentedThere is one other issue that node_export has, if you import a file that has no existing URI in the file_managed table, but the FID in the system you export collides with an entry to where you import too, node_export will overwrite the entry in the file_managed table on the postion of the exported FID. The expected behaviour would be to create a new entry within the file_managed table. The reason for this is that the URI check doesn't set the FID to NULL if it can't be found in the query.
Attached a patch to the earlier patch which should fix this behaviour.
Comment #2
kenorb CreditAttribution: kenorb commentedSlight change to be able to apply the patch.
Comment #3
kenorb CreditAttribution: kenorb commentedThe current patch didn't solve my issue reported at:
#2063121: Node Export: WD node: PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'uri'
Comment #4
kenorb CreditAttribution: kenorb commentedRelated:
#1804498: Import of files exported with 'fid' set fails
Comment #5
cinnamon CreditAttribution: cinnamon commenteda clean patch based on the above that seems to correct the problem with missing file attributes
Comment #6
jtwalters CreditAttribution: jtwalters commentedThe patch is #5 worked for my use case.
Maybe unrelated, but I have a custom HOOK_node_export_node_import_alter for preserving the original node id...
Comment #7
bendev CreditAttribution: bendev commented#5 worked for me
thanks
Comment #8
toiletfinder.com CreditAttribution: toiletfinder.com commentedI was having this issue as well. However, my problem was a result of the file folder not being writable. Please make sure that the appropriate file destination permissions are set.
Comment #9
JMC CreditAttribution: JMC commented#5 works for me too.
Apologies if I've changed the Status field incorrectly but I'm guessing "Patch (to be ported)" isn't correct?
Comment #10
njbarrett CreditAttribution: njbarrett as a volunteer commentedThis patched fixed the issue for me too.
Marking as RTBC.
Comment #11
broonJust to confirm it once more, #5 worked for me as well.
Comment #13
danielb CreditAttribution: danielb commented