0

It is the first time I'm using the migrate module and I want to import some nodes from a JSON file and to specify a unique key that has two distinct fields. I.e so that the row is not imported or it's updated when the combination of those two fields values already exists in the database

id: import_json_entities
label: "JSON Entities"
migration_group: import_json_group

destination:
  plugin: entity:node

migration_dependencies: { }

dependencies:
  enforced:
    module:
      - my_module

source:
  plugin: url
  data_fetcher_plugin: file
  data_parser_plugin: json

  urls:
    - 'public://entitities.json'

  item_selector: Schema

  ids:
    - name
    - version

  fields:
    -
      name: name
      label: 'Name'
      selector: Name
    -
      name: version
      label: 'Version'
      selector: Version


process:
  type:
    plugin: default_value
    default_value: service

  title: name
  uid:
    plugin: default_value
    default_value: 1

  field_version: version

It works if I only leave "name" in the list of ids, but it does not import anything if I specify both "name" and "version" as id.

What I want is something similar with what you achieve in MySQL with:

ALTER TABLE `entities` ADD UNIQUE `unique_index`(`name`, `version`);
2
  • Multiple key IDs are allowed and do work for me. I could imagine there is a conflict between the name key and the source field called name. Just guessing, but have you tried assigning another alias to for JSON source field Name?
    – Hudri
    Sep 23 '19 at 16:45
  • Thanks, I did, but without any luck. I guess I will try on a clean install as well. But what you are saying is that the syntax I'm using for multiple IDs is correct, isn't it?
    – Stefan
    Sep 24 '19 at 7:22
1

This is what I'm using in one of my imports:

source:
  ids:
    event_id:
      type: integer
    date_iso:
      type: string

I'm explicitly specifying the ID column type using type: integer or type: string.

1
  • Unfortunately this didn't help either, but thanks!
    – Stefan
    Sep 24 '19 at 7:55
1

Ok, it seems that the problem was not with the syntax, but with the fact that the table created in the database for my migration was corrupted (it had the wrong unique index). I had to manually delete migrate_map_import_json_entities and then reinstall the module (for some reason, uninstalling it didn't automatically deleted the table). Now it seems to work.

1
  • 2
    Oh, this is a known trap. Migrate module stores all imports in a source:target mapping table. If you change the IDs of your migrate.yml or remove your module, you are stuck with outdated mapping tables. See issue 2713327
    – Hudri
    Sep 24 '19 at 8:11

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.