Can't see nested elemets in API call

System Information
  • Strapi Version: 4.4.7
  • Operating System:
  • Database:
  • Node Version:
  • NPM Version:
  • Yarn Version:

I want to create relations between Post and Tag as follows.

  1. Post has many Tags
  2. Tags have and belongs to many Posts

I created collection types for Post and Tag.

Made them publicly available (for public user)

I created 1 post with 3 tags.

Now, when I try to see them at http://localhost:1337/api/posts
I don’t see nested elements, i.e. tags…
Am I missing anything ?

And lastly, I was not able to create Many-to-Many relations between Post and Tag with the following error:

You need to add the populate parameter to your REST API requests in order for Strapi to populate the relations.

1 Like

Thanks! This worked for Posts.
Could you please advise how to create relation wit Posts for Tag.
Currently, I can’t add relation with the error shown in my last screenshot.

That’s odd. Can you show your tag’s schema.json?

1 Like

Thanks.

In tag’s schema.json, there’s no relation yet, as I was not able to add in Strapi admin due to error.

{
  "kind": "collectionType",
  "collectionName": "tags",
  "info": {
    "singularName": "tag",
    "pluralName": "tags",
    "displayName": "Tag",
    "description": ""
  },
  "options": {
    "draftAndPublish": true
  },
  "pluginOptions": {},
  "attributes": {
    "name": {
      "type": "string",
      "required": true
    }
  }
}

Hmm looks fine to me. How about the post’s schema.json? Is there already a tags attribute?

Post’s schema.json looks fine.

{
  "kind": "collectionType",
  "collectionName": "posts",
  "info": {
    "singularName": "post",
    "pluralName": "posts",
    "displayName": "Post",
    "description": ""
  },
  "options": {
    "draftAndPublish": true
  },
  "pluginOptions": {},
  "attributes": {
    "title": {
      "type": "string",
      "required": true
    },
    "body": {
      "type": "text",
      "required": true
    },
    "tags": {
      "type": "relation",
      "relation": "oneToMany",
      "target": "api::tag.tag"
    }
  }
}

but I don’t see posts as relation against tag, when accessing http://localhost:1337/api/tags?populate[0]=posts

In this case, posts and tags supposed to be in many-to-many relations.
As Post has all 3 tags, in the screenshot below, eash tag should contain 1 post as nested element.

http://localhost:1337/api/posts?populate[0]=tags looks correct

Ah, your Post schema already contains a tags field which is defined as a oneToMany relation to Tag. The attribute names must be unique.

I see two ways you can change this.

1. UI Way

  • In the content type builder, remove the existing tags relation field in the post scheme
  • save
  • Now you’re free to create the many-to-many relation as you intended

2. Manual way

  • In your post’s schema.json, change the tags attribute to:
"tags": {
  "type": "relation",
  "relation": "manyToMany",
  "target": "api::tag.tag",
  "mappedBy": "tags"
}
  • In your tag’s schema.json, add a posts attribute:
"posts": {
  "type": "relation",
  "relation": "manyToMany",
  "target": "api::post.post",
  "inversedBy": "posts"
}

EDIT:
Actually you can just use the content type builder to change the existing oneToMany relation in the Post to a manyToMany relation, lol. After saving, if you check the other side of that relation (Tag), you will find that it has been added by Strapi automatically.

Thanks a lot!
Yes, I changed the relations in Post from one-to-many to many-to-many.

1 Like