Strapi relationship doesn't return the correct hierarchy

Viewed 624

I'm trying to create the following relationship model:

Section has many categories
Categories has many sub-categories

So essentially we have three collections here:

  1. Section
  2. Category
  3. SubCategory

At first, I've defined the relationship between Section and Category:

enter image description here

Then, I've created a relationship between Category and SubCategory:

enter image description here

I've also populate those collections, and when I call http://localhost:3002/sections I get:

[
    {
        "id": 1,
        "name": "ambition",
        "published_at": "2020-11-20T10:21:08.000Z",
        "created_at": "2020-11-20T11:21:06.000Z",
        "updated_at": "2020-12-01T10:26:44.000Z",
        "description": null,
        "featured_image": null,
        "categories": [],
        "subcategories": []
    },
    {
        "id": 2,
        "name": "the_tanning_of_the_future",
        "published_at": "2020-11-20T10:21:19.000Z",
        "created_at": "2020-11-20T11:21:18.000Z",
        "updated_at": "2020-12-01T10:27:15.000Z",
        "description": null,
        "featured_image": null,
        "categories": [],
        "subcategories": []
    },
    {
        "id": 3,
        "name": "living_sustainable",
        "published_at": "2020-11-20T10:21:28.000Z",
        "created_at": "2020-11-20T11:21:26.000Z",
        "updated_at": "2020-12-01T10:27:31.000Z",
        "description": null,
        "featured_image": null,
        "categories": [],
        "subcategories": []
    },
    {
        "id": 4,
        "name": "creativity",
        "published_at": "2020-11-20T10:21:36.000Z",
        "created_at": "2020-11-20T11:21:34.000Z",
        "updated_at": "2020-12-04T10:53:32.000Z",
        "description": null,
        "featured_image": null,
        "categories": [
            {
                "id": 1,
                "name": "Archivio",
                "published_at": "2020-11-20T10:57:34.000Z",
                "created_at": "2020-11-20T11:22:48.000Z",
                "updated_at": "2020-12-04T10:50:50.000Z",
                "subcategory": null,
                "feature_image": null
            },
            {
                "id": 2,
                "name": "Colori",
                "published_at": "2020-11-20T10:57:37.000Z",
                "created_at": "2020-11-20T11:22:55.000Z",
                "updated_at": "2020-12-03T11:55:11.000Z",
                "subcategory": null,
                "feature_image": null
            },
            {
                "id": 3,
                "name": "Lifestyle",
                "published_at": "2020-11-20T10:57:39.000Z",
                "created_at": "2020-11-20T11:23:02.000Z",
                "updated_at": "2020-12-03T11:55:20.000Z",
                "subcategory": null,
                "feature_image": null
            }
        ],
        "subcategories": [
            {
                "id": 1,
                "name": "Inverno 21",
                "published_at": "2020-12-03T15:16:57.000Z",
                "created_at": "2020-12-03T11:52:13.000Z",
                "updated_at": "2020-12-03T15:16:57.000Z",
                "category": 1,
                "description": null
            },
            {
                "id": 2,
                "name": "Estate 21",
                "published_at": "2020-12-03T11:54:42.000Z",
                "created_at": "2020-12-03T11:54:36.000Z",
                "updated_at": "2020-12-03T15:14:04.000Z",
                "category": 1,
                "description": null
            }
        ]
    }
]

As you can see from the response, the relationship between Section and Category is working fine, but the relationship between Category and SubCategory not. Infact, I've defined a 1:n relationship between these models, but I get:

{
    "id": 4,
    "name": "creativity",
    "published_at": "2020-11-20T10:21:36.000Z",
    "created_at": "2020-11-20T11:21:34.000Z",
    "updated_at": "2020-12-04T10:53:32.000Z",
    "description": null,
    "featured_image": null,
    "categories": [
        {
            "id": 1,
            "name": "Archivio",
            "published_at": "2020-11-20T10:57:34.000Z",
            "created_at": "2020-11-20T11:22:48.000Z",
            "updated_at": "2020-12-04T10:50:50.000Z",
            "subcategory": null,
            "feature_image": null
        },

The other weird thing is that inside the dashboard I can actually associate the sub-categories to the selected Category, see:

enter image description here

so I should have as response:

{
    "id": 4,
    "name": "creativity",
    "published_at": "2020-11-20T10:21:36.000Z",
    "created_at": "2020-11-20T11:21:34.000Z",
    "updated_at": "2020-12-04T10:53:32.000Z",
    "description": null,
    "featured_image": null,
    "categories": [
        {
            "id": 1,
            "name": "Archivio",
            "published_at": "2020-11-20T10:57:34.000Z",
            "created_at": "2020-11-20T11:22:48.000Z",
            "updated_at": "2020-12-04T10:50:50.000Z",
            "subcategories": [
                ...
            ],
            "feature_image": null
        },

am I doing something wrong or there is an issue in the relationship engine of Strapi?

0 Answers
Related