Update existing node (not created by Migrate) via Migrate

Created on 3 March 2018, almost 7 years ago
Updated 22 August 2024, 4 months ago

Hello,
I have got existing Drupal 8.5 site with hundreds of nodes. With migrate, if i try to upgrade, new nodes are rather created. Node have a field which is being used as key however that seem to be not working. It seems that node are only updated if their record present in migrate_map_xyz. Any suggestions?

CSV data

serial,type
ABC123,Laptop
XYZ124,Laptop
QRS567,Desktop
πŸ’¬ Support request
Status

Fixed

Version

8.7 ⚰️

Component
MigrationΒ  β†’

Last updated 4 days ago

Created by

πŸ‡¨πŸ‡¦Canada ranavaibhav Toronto

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡©πŸ‡°Denmark ressa Copenhagen

    Great thread, it really helped me a lot. I was looking for a way to import multiple CSV-files, sharing a unique nid. I wanted to run multiple migrations, populating different fields of a content type. I really wanted to avoid concatenating all the CSV-files into a single CSV-file.

    The key was to also map the nid, as outlined by @mikeryan:

    process:
      nid: nid_to_update

    Where "nid_to_update" is a column in your CSV of the nids you're updating.

    That was all that was needed. No need to add extra overwrite_properties parameters.

  • πŸ‡«πŸ‡·France arnaud-brugnon

    #23 is problably the closest solution.

    Use entity_lookup plugin is probably the best solution (nid_to_update is problably the uggliest solution)

    One precision to make entity_lookup works like a charm : Don't miss one import key (otherwise entity lookup will fail).
    It means you will have to set entity_type, bundle, bundle_key and value_key.
    bundle_key may seems overkill but you have to define it.

    Full migration file here

    id: tax_classification_import
    label: Import tax classifications
    migration_group: product_import
    
    source:
      plugin: import_tax_classification
      data_fetcher_plugin: http
      data_parser_plugin: json
      verify: FALSE
      fields:
        - name: code
          label: 'Code'
          selector: productTaxClassification/code
        - name: taxRate
          label: 'Tax rate'
          selector: tax/taxRate
      ids:
        code:
          type: string
      request_options:
        verify: false
    
    process:
      tid:
        plugin: entity_lookup
        source: code
        entity_type: taxonomy_term
        bundle: tax_classification
        bundle_key: vid
        value_key: field_code
        ignore_case: true
        access_check: 0
      vid:
        plugin: default_value
        default_value: tax_classification
      name: code
      field_code: code
      status:
        plugin: default_value
        default_value: 1
      field_rate: taxRate
    
    destination:
      plugin: 'entity:taxonomy_term'
      overwrite_properties:
        - field_rate
    
  • I've tried to update already existing commerce products, but I had an error - "Duplicate entry '1045' for key 'PRIMARY': INSERT INTO "commerce_product"

    process:
    
      product_id:
        plugin: entity_lookup
        source: Nr
        entity_type: commerce_product
        bundle_key: type
        bundle: course
        value_key: field_course_nr_
        ignore_case: true
        access_check: 0
    

    What should I do here?

Production build 0.71.5 2024