Baboo7 / strapi-plugin-import-export-entries

Import/Export data from and to your database in just few clicks.
175 stars 86 forks source link

IdField only takes effect when importing the current model #162

Open glacierck opened 8 months ago

glacierck commented 8 months ago

Describe the bug When importing data involving relationship types, even if the other party of the relationship has defined idField, it still fails to import due to the lack of id field selection for creation

To Reproduce age_groups schema.json { "collectionName": "age_groups", "pluginOptions": { "import-export-entries": { "idField": "title" } }, "attributes": { "title": { "type": "string", "unique": true, "required": true, "maxLength": 8 } } }

import data [ { "id": 1, "title": "some", "genders": "female", "ages": [{"title" : "youth"}] }]

output "error": "insert into \"public\".\"age_groups\" (\"created_at\", \"created_by_id\", \"title\", \"updated_at\", \"updated_by_id\") values ($1, $2, $3, $4, $5) returning \"id\" - duplicate key value violates unique constraint \"age_groups_pkey\"",

Expected behavior ages are connected to age_groups model, I hope to import data that can be connected to existing data in a relationship rather than creating new age records

glacierck commented 8 months ago

I have made some modifications to the source code to address the issue I mentioned earlier. I'm not sure if it will introduce any unknown issues.

https://github.com/Baboo7/strapi-plugin-import-export-entries/blob/f48341931dc95b428d0bcaac85b7cf38b14be6ba/src/server/services/import/import.js#L92

const updateOrCreate = async (user, slug, data, idField = 'id') => {
  const relationAttributes = getModelAttributes(slug, { filterType: ['component', 'dynamiczone', 'media', 'relation'] });
  for (let attribute of relationAttributes) {
    data[attribute.name] = await updateOrCreateRelation(user, attribute, data[attribute.name]);
  }

  let entry;
  const model = getModel(slug);
  // fix
  const _idField = model?.pluginOptions?.['import-export-entries']?.idField || idField;

  if (model.kind === 'singleType') {
    entry = await updateOrCreateSingleType(user, slug, data, _idField);
  } else {
    entry = await updateOrCreateCollectionType(user, slug, data, _idField);
  }
  return entry;
};
glacierck commented 8 months ago

During the test, I discovered another issue. When importing relationship type attributes first and then non relationship attributes, the relationship attributes will be deleted, but the reverse order of operations will not cause overwriting or deletion.

Baboo7 commented 8 months ago

@glacierck version 1.23.1 should fix the issue. I updated how relations are retrieved from database in code

glacierck commented 8 months ago

@Baboo7 strapi-plugin-import-export-entries/src/server/services/import/import.js Has the revision here been considered

glacierck commented 8 months ago

@glacierck version 1.23.1 should fix the issue. I updated how relations are retrieved from database in code

Thank you very much, the new version is effective. Solved the problem of secondary import data overwriting old relationships.

Hope the next version can also solve another problem, so that I don't need to invade the source code anymore.