Baboo7 / strapi-plugin-import-export-entries

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

[BUG] Error when importing data for a Single type a required Component field #130

Open lrasata opened 1 year ago

lrasata commented 1 year ago

Describe the bug Importing data for a Single type with Components sometimes work but in the following example it thorws an error. The schema of the content type is pretty simple, but very hard to see why it doesnt work.

Steps to follow to reproduce the bug : 1 - Use the following json to create the banner component inside components/banner/information-banner.json :

{
  "collectionName": "components_banner_info_banners",
  "info": {
    "displayName": "informationBanner"
  },
  "options": {},
  "attributes": {
    "header": {
      "type": "string",
      "required": true
    },
    "text": {
      "type": "richtext",
      "required": false
    },
    "background": {
      "type": "media",
      "multiple": false,
      "required": true,
      "allowedTypes": [
        "images"
      ]
    }
  }
}

2- Use the following schema.json to create a 'Test' Single type :

{
  "kind": "singleType",
  "collectionName": "tests",
  "info": {
    "singularName": "test",
    "pluralName": "tests",
    "displayName": "Test",
    "description": ""
  },
  "options": {
    "draftAndPublish": true
  },
  "pluginOptions": {
    "i18n": {
      "localized": true
    }
  },
  "attributes": {
    "banner": {
      "type": "component",
      "repeatable": false,
      "pluginOptions": {
        "i18n": {
          "localized": true
        }
      },
      "component": "banner.information-banner",
      "required": true,
      "min": 1
    }
  }
}

3 - Enter data in source env to export. The following is an example of data entry exported :

{
    "version": 2,
    "data": {
        "api::test.test": {
            "1": {
                "id": 1,
                "createdAt": "2023-06-28T22:38:32.142Z",
                "updatedAt": "2023-06-28T23:06:57.574Z",
                "publishedAt": "2023-06-28T22:38:34.023Z",
                "locale": "en",
                "banner": [
                    7
                ],
                "localizations": [],
                "createdBy": null,
                "updatedBy": null
            }
        },
        "banner.information-banner": {
            "7": {
                "id": 7,
                "header": "header",
                "text": "description",
                "background": 1
            }
        },
        "plugin::upload.file": {
            "1": {
                "id": 1,
                "name": "...", 
                "alternativeText": null,
                "caption": null,
                "hash": "...", 
                "ext": ".jpg",
                "mime": "image/jpeg",
                "url": "...", 
                "createdAt": "2023-06-28T02:42:19.163Z",
                "updatedAt": "2023-06-28T02:42:19.163Z",
                "createdBy": null,
                "updatedBy": null
            }
        },
        "plugin::upload.folder": {}
    }
}

4 - Import the data above

Current error

[
    {
        "error": {
            "name": "ValidationError",
            "message": "banner field must have at least 1 items",
            "details": {
                "errors": [
                    {
                        "path": [
                            "banner"
                        ],
                        "message": "banner field must have at least 1 items",
                        "name": "ValidationError"
                    }
                ]
            },
            "level": "\u001b[31merror\u001b[39m",
            "timestamp": "2023-06-29 11:21:58.982"
        },
        "data": {
            "id": 1,
            "createdAt": "2023-06-28T22:38:32.142Z",
            "updatedAt": "2023-06-28T23:06:57.574Z",
            "publishedAt": "2023-06-28T22:38:34.023Z",
            "locale": "en",
            "banner": [
                7
            ],
            "localizations": [],
            "createdBy": null,
            "updatedBy": null
        }
    }
]

Expected behavior Should be imported without error

Additional context Add any other context about the problem here.

lrasata commented 1 year ago

I have tested a bit further and saw that in this case, because the component field is required, it throws an error. When changing the required field from true to false, it works without any problems

cj11489 commented 1 year ago

I have a slightly different error message, but ultimately appears to be the same issue. Any required fields, even if they are populated, will error on import. Was there a resolution to this? Changing required from true to false isn't really an option for me.

Screenshot 2023-10-13 at 11 07 48
Baboo7 commented 11 months ago

I strongly suspect this is because one of the required field is a relation. Since data is imported in two steps (first the scalar attributes, then the relations), import fails on the first step.

As you mentionned @lrasata, changing the relation attribute to not required will most likely solve the issue 👍

cj11489 commented 11 months ago

@Baboo7 there are no relations in my model. Its a relatively straight forward model, that contains text, rich text, enumerations, images and repeatable components. Most of the fields will be required. Any chance you would be able to take a look into this please?

Baboo7 commented 11 months ago

Actually images are relations since they are saved and linked from another table. I think the issue comes from this specific field