Unable to import due to "Field 'sourceid2' doesn't have a default value" I am using the awesome module Migrate, but I have problem, which I am unable to solve. I try to migrate articles from custom PHP site with MySQL DB to D7 with MySQL. The generated SQL query from my migration is ok when I run it in my MySQL DB using MySQL CLI. But when I try to migrate it using the Migrate UI, I get an error:


  An AJAX HTTP error occurred. HTTP Result Code: 500 Debugging
  information follows. Path: /batch?id=113&amp;op=do StatusText: Service
  unavailable (with message) ResponseText: PDOException:
  SQLSTATE[HY000]: General error: 1364 Field 'sourceid2' doesn't have a
  default value: INSERT INTO {migrate_map_geografbgarticlenode}
  (sourceid1, needs_update, rollback_action, hash) VALUES
  (:db_insert_placeholder_0, :db_insert_placeholder_1,
  :db_insert_placeholder_2, :db_insert_placeholder_3); Array (
  [:db_insert_placeholder_0] => 2 [:db_insert_placeholder_1] => 3
  [:db_insert_placeholder_2] => 0 [:db_insert_placeholder_3] => ) in
  MigrateSQLMap->saveIDMapping() (line 434 of
  ~/drupal/dev/sites/all/modules/migrate/plugins/sources/sqlmap.inc).


It seems the problem is with my MigrateSQLMap function, looks like N.id is NULL when the query is run by Drupal. But I triple checked it before posting, it's not nullable field in DB and in the generate query result. Any idea how can I fix it?

$query = Database::getConnection('default', 'legacy')
      -&gt;select('news', 'N')
      -&gt;fields('N', array('id', 'category_id', 'createdon', 'updatedon', 'createdby', 'updatedby'))
      -&gt;fields('L', array('code'))
      -&gt;fields('NT', array('name', 'short_description', 'description'))
      -&gt;fields('G', array('id', 'description'));
    $query-&gt;leftJoin('news_translations', 'NT', 'NT.target_id = N.id');
    $query-&gt;leftJoin('languages', 'L', 'NT.language_id = L.id');
    $query-&gt;leftJoin('tags', 'T', 'T.news_id = N.id');
    $query-&gt;leftJoin('gallery', 'G', 'G.target_id = N.id');
    $query-&gt;addExpression('GROUP_CONCAT(LOWER(T.tag))', 'tags');
    $query-&gt;groupBy('N.id');
    $query-&gt;orderBy('N.id', 'ASC');

    $this-&gt;source = new MigrateSourceSQL($query);
    $this-&gt;destination = new MigrateDestinationNode('article');

    $this-&gt;map = new MigrateSQLMap($this-&gt;machineName,
        array(
          'id' =&gt; array(
            'type' =&gt; 'int',
            'unsigned' =&gt; TRUE,
            'not null' =&gt; TRUE,
            'description' =&gt; t('Article Source ID'),
            'alias' =&gt; 'N',
          ),
        ),
        MigrateDestinationNode::getKeySchema()
      );

    //... not relevant
Ok, I resolved this issue. I am new to Migrate module and I think the message was not clear enough.

First I have mapped to complex foreign key with two columns:

&lt;?php
    $this-&gt;map = new MigrateSQLMap($this-&gt;machineName,
        array(
          'id' =&gt; array(
            'type' =&gt; 'int',
            'description' =&gt; t('Article Source ID'),
            'alias' =&gt; 'N',
          ),
          'id2' =&gt; array( // no longer exists in my code
            'type' =&gt; 'int',
            'description' =&gt; t('Article Source ID'),
            'alias' =&gt; 'N',
          ),
        ),
        MigrateDestinationNode::getKeySchema()
      );
?&gt;


But then I used only the first foreign key. The database table storing the mapping between source and destination had two columns sourceId, because of first attempt to run migration, but hasn't updated with the new migration in the code.

I dropped the migration_map table and everything was freshly rebuild and works like a charm.