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():

<?php
 $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:

<?php
$newfile
= (object)array_merge((array)$oldfile,(array)$file);
?>

Patch is attached, please review my fix.

Comments

olklein’s picture

StatusFileSize
new837 bytes

There 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.

kenorb’s picture

StatusFileSize
new890 bytes

Slight change to be able to apply the patch.

kenorb’s picture

kenorb’s picture

cinnamon’s picture

Status:Needs review» Patch (to be ported)
StatusFileSize
new522 bytes

a clean patch based on the above that seems to correct the problem with missing file attributes

jtwalters’s picture

The 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...

bendev’s picture

#5 worked for me
thanks

toiletfinder.com’s picture

I 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.

JMC’s picture

Status:Patch (to be ported)» Needs review

#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?