0

I am currently working on migrating content from a Drupal 6 site to Drupal 7 and have started with a single content type for testing. The source content type has an optional image field, however if I try to import only those nodes with a file attached, I'm still running into issues.

Here is the source sql:

// Get source data:
//   1. node data
$query = Database::getConnection('default', 'extra')
  ->select('node', 'n')
  ->fields('n', array('nid', 'title', 'created', 'changed', 'status', 'promote', 'sticky', 'language', 'tnid', 'comment'))
  ->condition('type', 'guest_lecturer');
//   2. node revision data (body, etc)
$query->join('node_revisions', 'nr', 'nr.vid = n.vid');
$query->fields('nr', array('body', 'format', 'log'));
//   3. extra fields
$query->join('content_type_guest_lecturer', 'nt', 'n.nid = nt.nid');
$query->fields('nt', array('field_lecturer_title_value', 'field_lecturer_teaser_value', 'field_lecturer_featured_value'));
//   4. files (only nodes with this file field set)
$query->join('files', 'f', 'f.fid = nt.field_lecturer_photo_fid');
$query->fields('f', array('filename'));
//   4. files (all nodes)
//$query->leftJoin('files', 'f', 'f.fid = nt.field_lecturer_photo_fid');
//$query->fields('f', array('filename'));
//   5. url alias
$query->join('url_alias', 'a', "a.src = CONCAT('node/', n.nid)");
$query->addField('a', 'dst');

And the field mapping:

// add field mappings
$this->addFieldMapping('title', 'title');
$this->addFieldMapping('created', 'created');
$this->addFieldMapping('changed', 'changed');
$this->addFieldMapping('status', 'status');
$this->addFieldMapping('promote', 'promote');
$this->addFieldMapping('sticky', 'sticky');
$this->addFieldMapping('language', 'language');
$this->addFieldMapping('tnid', 'tnid');
$this->addFieldMapping('comment', 'comment');
$this->addFieldMapping('uid')->defaultValue(1);
$this->addFieldMapping('revision')->defaultValue(FALSE);
$this->addFieldMapping('log', 'log');
$this->addFieldMapping('translate')->defaultValue(FALSE);
$this->addFieldMapping('revision_uid')->defaultValue(1);
$this->addFieldMapping('is_new')->defaultValue(FALSE);
$this->addFieldMapping('path', 'dst');

// add photo field mappings
$this->addFieldMapping('field_lecturer_photo', 'filename');
$this->addFieldMapping('field_lecturer_photo:file_class')->defaultValue('MigrateFileUri');
$this->addFieldMapping('field_lecturer_photo:language')->defaultValue('und');
$this->addFieldMapping('field_lecturer_photo:destination_dir')->defaultValue('sites/default/files');
$this->addFieldMapping('field_lecturer_photo:source_dir')->defaultValue('sites/all/modules/custom/azamara_migrate/files/lecturer_photo');
$this->addFieldMapping('field_lecturer_photo:destination_file', 'filename');
$this->addFieldMapping('field_lecturer_photo:file_replace')->defaultValue('FILE_EXISTS_REPLACE');
$this->addFieldMapping('field_lecturer_photo:preserve_files')->defaultValue(FALSE);
$this->addFieldMapping('field_lecturer_photo:alt')->defaultValue('');
$this->addFieldMapping('field_lecturer_photo:title')->defaultValue('');

And the error when I try to run the import:

22911   Error   PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id' 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] => 92 [:db_insert_placeholder_1] => file [:db_insert_placeholder_2] => node [:db_insert_placeholder_3] => [:db_insert_placeholder_4] => 1 ) in file_usage_add() (line 696 of /htdocs_svn/includes/file.inc).
22911   Error   SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id' 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] => 92 [:db_insert_placeholder_1] => file [:db_insert_placeholder_2] => node [:db_insert_placeholder_3] => [:db_insert_placeholder_4] => 1 ) (/htdocs_svn/includes/file.inc:696)

I can see that the [:db_insert_placeholder_3] is empty and is causing the problem, but how do I correct this?

  • Is there anyway you can download images using http url? – heshanlk Nov 10 '14 at 16:50
1

Changing this line from:

$this->addFieldMapping('is_new')->defaultValue(FALSE);

to:

$this->addFieldMapping('is_new')->defaultValue(TRUE);

fixes this specific issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.